Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.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#_Types_Return_Overriding_Abstract - Fatal编程技术网

C# 用具体类型重写抽象方法返回类型

C# 用具体类型重写抽象方法返回类型,c#,types,return,overriding,abstract,C#,Types,Return,Overriding,Abstract,我正在尝试这样做: public abstract class A { public abstract AbstractSomething foobar {get;} } public class B : A { public override ConcreteSomething foobar { get { return new ConcreteSomething(); } } } 编译

我正在尝试这样做:

public abstract class A
{
    public abstract AbstractSomething foobar {get;}
}

public class B : A
{
    public override ConcreteSomething foobar
    {
        get
        {
            return new ConcreteSomething();
        }
    }
}
编译器警告我重写类型必须与基类型匹配。我对返回AbstractSomething的关注是,我要求用户正确地转换返回值

实现我的getter的最佳方法是什么?不使用泛型就可以做到这一点吗

不使用泛型就可以做到这一点吗

没有。使用泛型:

public abstract class A<T>
    where T : AbstractSomething
{
    public abstract T foobar { get; }
}

public class B : A<ConcreteSomething>
{
    public override ConcreteSomething foobar
    {
        get
        {
            return new ConcreteSomething();
        }
    }
}
公共抽象类A
T:什么
{
公共摘要T foobar{get;}
}
B级:A级
{
公共覆盖在foobar上
{
收到
{
返回新事物();
}
}
}
不使用泛型就可以做到这一点吗

没有。使用泛型:

public abstract class A<T>
    where T : AbstractSomething
{
    public abstract T foobar { get; }
}

public class B : A<ConcreteSomething>
{
    public override ConcreteSomething foobar
    {
        get
        {
            return new ConcreteSomething();
        }
    }
}
公共抽象类A
T:什么
{
公共摘要T foobar{get;}
}
B级:A级
{
公共覆盖在foobar上
{
收到
{
返回新事物();
}
}
}

您确实需要使用泛型,但这不是一个糟糕的解决方案:

public abstract class A<T> where T:AbstractSomething
{
    public abstract T foobar { get; }
}

public abstract class AbstractSomething
{
}

public class ConcreteSomething : AbstractSomething
{

}

public class B : A<ConcreteSomething>
{
    public override ConcreteSomething foobar
    {
        get
        {
            return new ConcreteSomething();
        }
    }
}
公共抽象类A,其中T:AbstractSomething
{
公共摘要T foobar{get;}
}
公共抽象类抽象的东西
{
}
公共类具体化:抽象化
{
}
B级:A级
{
公共覆盖在foobar上
{
收到
{
返回新事物();
}
}
}

您确实需要使用泛型,但这不是一个糟糕的解决方案:

public abstract class A<T> where T:AbstractSomething
{
    public abstract T foobar { get; }
}

public abstract class AbstractSomething
{
}

public class ConcreteSomething : AbstractSomething
{

}

public class B : A<ConcreteSomething>
{
    public override ConcreteSomething foobar
    {
        get
        {
            return new ConcreteSomething();
        }
    }
}
公共抽象类A,其中T:AbstractSomething
{
公共摘要T foobar{get;}
}
公共抽象类抽象的东西
{
}
公共类具体化:抽象化
{
}
B级:A级
{
公共覆盖在foobar上
{
收到
{
返回新事物();
}
}
}

您可以使用泛型来指定它,尽管这会使某些事情更难处理:

public abstract class A<T> where T : AbstractSomething
{
    public abstract T foobar {get;}
}

public class B : A<ConcreteSomething>
{
    public override ConcreteSomething foobar
    {
        get
        {
            return new ConcreteSomething();
        }
    }
}
公共抽象类A,其中T:AbstractSomething
{
公共摘要T foobar{get;}
}
B级:A级
{
公共覆盖在foobar上
{
收到
{
返回新事物();
}
}
}

您可以使用泛型来指定它,尽管这会使某些事情更难处理:

public abstract class A<T> where T : AbstractSomething
{
    public abstract T foobar {get;}
}

public class B : A<ConcreteSomething>
{
    public override ConcreteSomething foobar
    {
        get
        {
            return new ConcreteSomething();
        }
    }
}
公共抽象类A,其中T:AbstractSomething
{
公共摘要T foobar{get;}
}
B级:A级
{
公共覆盖在foobar上
{
收到
{
返回新事物();
}
}
}

这里不使用接口的原因是什么?如果您使用的是抽象方法,那么第一点是您应该在派生类中定义抽象方法。为什么不使用泛型?为什么用户必须强制转换它?如果他引用了你的
B
,但只知道
a
,因为否则他不需要基类,他不能期望得到一个
具体的东西
,而只能得到一个
抽象的东西
(这就是抽象类
a
承诺给他的),所以他根本不需要抛出它,您可以在override getter中返回抽象类。要添加到pascalhein的答案中,如果您的调用者希望使用比AbstractSomething更具功能的类执行某些操作,那么这不是一个合适的模式。有没有理由不在这里使用接口?如果您使用的是抽象方法,那么第一点是您应该在派生类中定义抽象方法。为什么不使用泛型?为什么用户必须强制转换它?如果他引用了你的
B
,但只知道
a
,因为否则他不需要基类,他不能期望得到一个
具体的东西
,而只能得到一个
抽象的东西
(这就是抽象类
a
承诺给他的),所以他根本不需要抛出它,您可以在override getter中返回抽象类。要添加到pascalhein的答案中,如果调用方希望使用比AbstractSomething功能更多的类来执行某些操作,那么这不是一个合适的模式。