[英]Child class with method of covariant return type, workaround?
I'm trying to neatly organize my code and came across and oddity as primarily Java-focused programmer. 我试图整洁地组织我的代码,并且遇到了很多以Java为主的程序员。 Here's the simplified version: 这是简化版:
class ReturnParent { ... }
class ReturnChild : ReturnParent { ... }
class Parent
{
abstract ReturnParent SomeMethod();
}
class Child : Parent
{
// Note the return type
override ReturnChild SomeMethod() {}
}
Now I've looked up this issue and I know this doesn't work in C# because covariance isn't supported in this way. 现在,我查找了此问题,并且我知道这在C#中不起作用,因为不支持以这种方式进行协方差。 My question is, is there a workaround to this issue? 我的问题是,是否有解决此问题的方法?
The method can be a parameter instead; 该方法可以改为参数。 the classes, including the return classes, MUST be classes, they cannot be interfaces; 这些类(包括返回类)必须是类,它们不能是接口; they can implement interfaces though, if that helps. 他们可以实现接口,如果有帮助的话。
Also, the main class must be castable, which was an issue when I tried generics: 另外,主类必须是可转换的,这在我尝试泛型时是一个问题:
Parent p = new Child(); // This works
Child c = p as Child; // This doesn't work with generics
You can let the subclasses decide the return type of SomeMethod
. 您可以让子类确定SomeMethod
的返回类型。
abstract class Parent<TReturn> where TReturn : ReturnParent
{
public abstract TReturn SomeMethod();
}
class Child : Parent<ReturnChild>
{
public override ReturnChild SomeMethod(){}
}
If you make Parent<T>
implement the following covariant interface, then you could cast Child
to IParent<ReturnParent>
. 如果使Parent<T>
实现以下协变接口,则可以将Child
IParent<ReturnParent>
为IParent<ReturnParent>
。
interface IParent<out T>
{
T SomeMethod();
}
If you want to make it castable specifically to Parent<ReturnParent>
, then no, that can't be done, and you may need a redesign. 如果要使其特定于Parent<ReturnParent>
到Parent<ReturnParent>
,则不能,那是不可能的,您可能需要重新设计。
This works with Linqpad 与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.