C# 允许接口返回C中的类#
接口有没有一种方法可以返回空值和类 接口C# 允许接口返回C中的类#,c#,design-patterns,interface,C#,Design Patterns,Interface,接口有没有一种方法可以返回空值和类 接口 public interface IAnimalSlide<C> where <C>:ImageViewer { void endShow(); imageViewer displayImage(); void setNewImage(); } 图像幻灯片 public class SlideShow { IAnimalSlide = prevSlide; IAnimalSlide = nextSlid
public interface IAnimalSlide<C> where <C>:ImageViewer
{
void endShow();
imageViewer displayImage();
void setNewImage();
}
图像幻灯片
public class SlideShow
{
IAnimalSlide = prevSlide;
IAnimalSlide = nextSlide;
List<IAnimalSlide> animalList;
}
...
animalList.catList.ToList<IAnimalSlide>(); //where catList is a list of Cats
Canvas.showImage(IAnimalSlide.displayImage());
公共类幻灯片放映
{
IAnimalSlide=前滑;
IAnimalSlide=下一个Slide;
列出动物学家;
}
...
animalist.catList.ToList()//其中catList是猫的列表
showImage(IAnimalSlide.displayImage());
我现在收到一个错误,上面写着
使用泛型类型“IAnimalSlide”需要1种类型的参数
ImageSlideShow调用了一个名为ImageViewer的类,我想在接口中调用它。有没有办法将该类合并到接口中 这里有几件事。首先,泛型接口不使用泛型类型参数
C
。如果要使用从ImageViewer
派生的不同类,则应使用以下内容:
public interface IAnimalSlide<TImageViewer>
where TImageViewer : ImageViewer
{
void endShow();
void setNewImage();
TImageViewer displayImage();
}
然而,您似乎只想使用ImageViewer
,而不会使用从它派生的类,在这种情况下,您不需要通用接口;只需删除泛型类型A片段,并为displayImage
方法指定ImageViewer
返回类型:
public interface IAnimalSlide
{
void endShow();
void setNewImage();
ImageViewer displayImage();
}
基于代码的其余部分,您将遇到其他问题,但这将使您克服问题中的错误
使用泛型类型“IAnimalSlide”需要1种类型的参数
该错误是因为无论何时使用泛型IAnimalSlide
都需要声明编译时类型:
IAnimalSlide<ImageViewer> slide;
IAnimalSlide幻灯片;
但不清楚你到底在问什么。通过从接口返回类型,可以“允许接口返回类型”:
public interface IAnimalSlide
{
void EndShow();
ImageViewer DisplayImage(); // <-- Note the casing of the return type
void SetNewImage();
}
公共接口IAnimalSlide
{
void EndShow();
ImageViewer DisplayImage();//谢谢您在最后的注释。
IAnimalSlide<ImageViewer> slide;
public interface IAnimalSlide
{
void EndShow();
ImageViewer DisplayImage(); // <-- Note the casing of the return type
void SetNewImage();
}