Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
抽象类中的C#抽象方法,子类返回不同类型_C#_Inheritance_Abstract Class_Abstraction_Abstract Methods - Fatal编程技术网

抽象类中的C#抽象方法,子类返回不同类型

抽象类中的C#抽象方法,子类返回不同类型,c#,inheritance,abstract-class,abstraction,abstract-methods,C#,Inheritance,Abstract Class,Abstraction,Abstract Methods,我理解这里发布的解决方案 但是,我不认为我可以使用泛型,因为其他一些类包含“Content”,我不想决定它在编译时使用哪种类型的内容 public abstract class Content { public abstract Content getContent(); } public class UrlContent : Content { public String s; public getContent(){ return s;} } public c

我理解这里发布的解决方案

但是,我不认为我可以使用泛型,因为其他一些类包含“Content”,我不想决定它在编译时使用哪种类型的内容

public abstract class Content {
    public abstract Content getContent();
}

public class UrlContent : Content  {
    public String s;
    public getContent(){ return s;}

}

public class ImageContent : Content  {
    public Byte[] image;
    public getContent(){ return image;}
}

您可以使用实现这一点,并且仍然维护一个非通用接口,您可以在不需要知道返回类型的任何地方引用该接口,例如:

public interface IContent {
    object GetContent();
}

public abstract class Content<T> : IContent {
    public abstract T GetContent();
    object IContent.GetContent() 
    {
        return this.GetContent(); // Calls the generic GetContent method.
    }
}

public class UrlContent : Content<String>  {
    public String s;
    public override String GetContent() { return s; }
}

public class ImageContent : Content<Byte[]>  {
    public Byte[] image;
    public override Byte[] GetContent(){ return image; }
}
公共接口IContent{
对象GetContent();
}
公共抽象类内容:IContent{
公共摘要T GetContent();
对象IContent.GetContent()
{
返回此。GetContent();//调用通用的GetContent方法。
}
}
公共类UrlContent:Content{
公共字符串s;
公共重写字符串GetContent(){return s;}
}
公共类ImageContent:内容{
公共字节[]图像;
公共重写字节[]GetContent(){return image;}
}

您可以使用实现这一点,并且仍然可以维护一个非通用接口,您可以在不需要知道返回类型的任何地方引用该接口,例如:

public interface IContent {
    object GetContent();
}

public abstract class Content<T> : IContent {
    public abstract T GetContent();
    object IContent.GetContent() 
    {
        return this.GetContent(); // Calls the generic GetContent method.
    }
}

public class UrlContent : Content<String>  {
    public String s;
    public override String GetContent() { return s; }
}

public class ImageContent : Content<Byte[]>  {
    public Byte[] image;
    public override Byte[] GetContent(){ return image; }
}
公共接口IContent{
对象GetContent();
}
公共抽象类内容:IContent{
公共摘要T GetContent();
对象IContent.GetContent()
{
返回此。GetContent();//调用通用的GetContent方法。
}
}
公共类UrlContent:Content{
公共字符串s;
公共重写字符串GetContent(){return s;}
}
公共类ImageContent:内容{
公共字节[]图像;
公共重写字节[]GetContent(){return image;}
}

假设后面的实现是
公共覆盖内容getContent()
方法

您已将函数声明为返回内容对象,然后尝试从中返回字符串或字节[]。这行不通。如果您希望能够返回任意数量的类型,最简单的方法是返回
对象
,而不是
内容
,这对于字节[]或字符串很好。它还警告调用代码数据类型可以是任何类型


您还可以像在该链接中引用的那样使用泛型,让不返回特定类型的类将自己定义为
公共类奇怪内容:Content
,这样它们可以返回多个数据类型,而不必强制所有实现都是松散类型的。

假设后面的实现是be
public重写内容getContent()
方法

您已将函数声明为返回内容对象,然后尝试从中返回字符串或字节[]。这行不通。如果您希望能够返回任意数量的类型,最简单的方法是返回
对象
,而不是
内容
,这对于字节[]或字符串很好。它还警告调用代码数据类型可以是任何类型


您还可以像在该链接中引用的那样使用泛型,让不返回特定类型的类将自己定义为
公共类奇怪的内容:Content
,这样它们可以返回多个数据类型,而不必强制所有实现都是松散类型的。

但是现在每次我声明它需要的新内容时要键入。@user3373235否,您可以声明一个类型为
IContent
的变量,它不是泛型的。但现在每次我声明一个新内容时,它都需要键入。@user3373235否,您可以声明一个类型为
IContent
的变量,它不是泛型的。您是否询问是否可以避免使用泛型?我不清楚你的问题是什么。我也不确定,但p.s.w.g的解决方案解决了我的问题。你是在问你是否可以避免使用泛型?我不清楚你的问题是什么。我也不确定,但p.s.w.g的解决方案解决了我的问题。