![](/img/trans.png)
[英]Using interface with contravariant type as a return type of a method in an interface with covariant type
[英]Child class with method of covariant return type, workaround?
我试图整洁地组织我的代码,并且遇到了很多以Java为主的程序员。 这是简化版:
class ReturnParent { ... }
class ReturnChild : ReturnParent { ... }
class Parent
{
abstract ReturnParent SomeMethod();
}
class Child : Parent
{
// Note the return type
override ReturnChild SomeMethod() {}
}
现在,我查找了此问题,并且我知道这在C#中不起作用,因为不支持以这种方式进行协方差。 我的问题是,是否有解决此问题的方法?
该方法可以改为参数。 这些类(包括返回类)必须是类,它们不能是接口; 他们可以实现接口,如果有帮助的话。
另外,主类必须是可转换的,这在我尝试泛型时是一个问题:
Parent p = new Child(); // This works
Child c = p as Child; // This doesn't work with generics
您可以让子类确定SomeMethod
的返回类型。
abstract class Parent<TReturn> where TReturn : ReturnParent
{
public abstract TReturn SomeMethod();
}
class Child : Parent<ReturnChild>
{
public override ReturnChild SomeMethod(){}
}
如果使Parent<T>
实现以下协变接口,则可以将Child
IParent<ReturnParent>
为IParent<ReturnParent>
。
interface IParent<out T>
{
T SomeMethod();
}
如果要使其特定于Parent<ReturnParent>
到Parent<ReturnParent>
,则不能,那是不可能的,您可能需要重新设计。
与Linqpad一起使用
abstract class Parent<T>
{
public abstract T SomeMethod();
}
class Child : Parent<ReturnChild>
{
// Note the return type
public override ReturnChild SomeMethod()
{
return new ReturnChild();
}
}
void Main()
{
var p = new Child(); // This works
Child c = p as Child; // This doesn't work with generics
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.