C#:重写返回类型
有没有办法重写 C# 中的返回类型? 如果是的话,如何进行?如果不是,为什么以及推荐的做法是什么?
我的情况是,我有一个带有抽象基类及其后代的接口。 我想这样做(好吧,不是真的,但作为一个例子!):
public interface Animal
{
Poo Excrement { get; }
}
public class AnimalBase
{
public virtual Poo Excrement { get { return new Poo(); } }
}
public class Dog
{
// No override, just return normal poo like normal animal
}
public class Cat
{
public override RadioactivePoo Excrement { get { return new RadioActivePoo(); } }
}
RadioactivePoo
当然继承自 Poo
。
我想要这个的原因是,那些使用 Cat
对象的人可以使用 Excrement
属性,而不必将 Poo
转换为 RadioactivePoo
例如,猫
仍然可能是动物
列表的一部分,其中用户可能不一定知道或关心他们的放射性粪便。 希望这是有道理的......
据我所知,编译器至少不允许这样做。 所以我想这是不可能的。 但您会推荐什么解决方案呢?
Is there way to override return types in C#? If so how, and if not why and what is a recommended way of doing it?
My case is that I have an interface with an abstract base class and descendants of that. I would like to do this (ok not really, but as an example!) :
public interface Animal
{
Poo Excrement { get; }
}
public class AnimalBase
{
public virtual Poo Excrement { get { return new Poo(); } }
}
public class Dog
{
// No override, just return normal poo like normal animal
}
public class Cat
{
public override RadioactivePoo Excrement { get { return new RadioActivePoo(); } }
}
RadioactivePoo
of course inherits from Poo
.
My reason for wanting this is so that those who use Cat
objects could use the Excrement
property without having to cast the Poo
into RadioactivePoo
while for example the Cat
could still be part of an Animal
list where users may not necessarily be aware or care about their radioactive poo. Hope that made sense...
As far as I can see the compiler doesn't allow this at least. So I guess it is impossible. But what would you recommend as a solution to this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
通用基类怎么样?
编辑:一个新的解决方案,使用扩展方法和标记接口...
这样 Dog 和 Cat 都继承自 Animal (正如评论中所述,我的第一个解决方案没有保留继承)。< br>
有必要使用标记接口显式标记类,这很痛苦,但这也许可以给您一些想法...
第二次编辑 @Svish:我修改了代码以明确显示扩展方法不以任何方式强制执行
iPooProvider
继承自BaseAnimal
的事实。 “更强类型”是什么意思?What about a generic base class?
EDIT: A new solution, using extension methods and a marker interface...
This way Dog and Cat both inherit from Animal (as remarked in the comments, my first solution did not preserve the inheritance).
It's necessary to mark explicitly the classes with the marker interface, which is painful, but maybe this could give you some ideas...
SECOND EDIT @Svish: I modified the code to show explitly that the extension method is not enforcing in any way the fact that
iPooProvider
inherits fromBaseAnimal
. What do you mean by "even more strongly-typed"?我知道这个问题已经有很多解决方案,但我想我已经想出了一个可以解决我在现有解决方案中遇到的问题的解决方案。
我对现有的一些解决方案并不满意,原因如下:
我的解决方案
该解决方案应该通过使用泛型和方法隐藏来克服我上面提到的所有问题。
使用此解决方案,您不需要覆盖 Dog OR Cat 中的任何内容! 这是一些示例用法:
这将输出:“RadioactivePoo”两次,这表明多态性尚未被破坏。
进一步阅读
MyType
并从 BaseAnimal 返回MyType
的操作,那么您会需要使用它才能在两者之间进行转换。I know there are a lot of solutions for this problem already but I think I've come up with one that fixes the issues I had with the existing solutions.
I wasn't happy with the some of the existing solutions for the following reasons:
My Solution
This solution should overcome all of the issues I mentioned above by using both generics and method hiding.
With this solution you don't need to override anything in Dog OR Cat! Here is some sample usage:
This will output: "RadioactivePoo" twice which shows that polymorphism has not been broken.
Further Reading
MyType<Poo>
from IAnimal and returnMyType<PooType>
from BaseAnimal then you would need to use it to be able to cast between the two.这称为返回类型协方差,通常在 C# 或 .NET 中不支持,尽管有些人愿望。
我要做的就是保留相同的签名,但向派生类添加一个额外的 ENSURE 子句,以确保该子句返回一个 RadioActivePoo。 所以,简而言之,我会通过契约设计来完成我无法通过语法完成的事情。
其他人则更喜欢伪造它。 我想这没问题,但我倾向于节省“基础设施”代码行。 如果代码的语义足够清晰,我很高兴,并且按契约设计可以让我实现这一点,尽管它不是编译时机制。
对于泛型也是如此,其他答案建议这样做。 我使用它们的原因不仅仅是归还放射性粪便——但这只是我的想法。
This is called return type covariance and is not supported in C# or .NET in general, despite some people's wishes.
What I would do is keep the same signature but add an additional
ENSURE
clause to the derived class in which I ensure that this one returns aRadioActivePoo
. So, in short, I'd do via design by contract what I can't do via syntax.Others prefer to fake it instead. It's ok, I guess, but I tend to economize "infrastructure" lines of code. If the semantics of the code are clear enough, I'm happy, and design by contract lets me achieve that, although it is not a compile time mechanism.
The same for generics, which other answers suggest. I would use them for a better reason than just returning radioactive poo - but that's just me.
C#9 为我们提供了协变覆盖返回类型。 基本上:你想要的就能工作。
C#9 gives us covariant override return types. Basically: what you want just works.
还有这个选项(显式接口实现)
您失去了使用基类来实现 Cat 的能力,但从好的方面来说,您保留了 Cat 和 Dog 之间的多态性。
但我怀疑增加的复杂性是否值得。
There is also this option (explicit interface-implementation)
You lose the ability to use the base-class to implement Cat, but on the plus-side, you keep the polymorphism between Cat and Dog.
But I doubt the added complexity is worth it.
为什么不定义一个受保护的虚拟方法来创建“排泄物”并保留返回“排泄物”的公共属性非虚拟。 然后派生类可以覆盖基类的返回类型。
在下面的示例中,我将“Excrement”设为非虚拟,但提供属性 ExcrementImpl 以允许派生类提供正确的“Poo”。 然后,派生类型可以通过隐藏基类实现来覆盖“Excrement”的返回类型。
前任:
Why not define a protected virtual method that creates the 'Excrement' and keep the public property that returns the 'Excrement' non virtual. Then derived classes can override the return type of the base class.
In the following example, I make 'Excrement' non-virtual but provide the property ExcrementImpl to allow derived classes to provide the proper 'Poo'. Derived types can then override the return type of 'Excrement' by hiding the base class implementation.
E.x.:
尝试这个:
Try this:
如果我错了,请纠正我,但多态性的全部意义不是能够返回 RadioActivePoo(如果它继承自 Poo),合约将与抽象类相同,但只返回 RadioActivePoo()
Correct me if im wrong but isnt the whole point of pollymorphism to be able to return RadioActivePoo if it inherits from Poo, the contract would be the same as the abstract class but just return RadioActivePoo()
我想我已经找到了一种不依赖于泛型或扩展方法,而是依赖于方法隐藏的方法。 但是,它可能会破坏多态性,因此如果您进一步继承 Cat,请特别小心。
我希望这篇文章仍然可以帮助别人,尽管晚了 8 个月。
I think I've found a way that doesn't depend on generics or extension methods, but rather method hiding. It can break polymorphism, however, so be especially careful if you further inherit from Cat.
I hope this post could still help somebody, despite being 8 months late.
如果 RadioactivePoo 是从 poo 派生出来的,然后使用泛型,这可能会有所帮助。
It might help if RadioactivePoo is derived from poo and then use generics.
我相信你的答案称为协方差。
I believe your answer is called covariance.
您可以只使用返回接口。 就你而言,IPoo。
在您的情况下,这比使用泛型类型更可取,因为您正在使用注释基类。
You could just use a return an Interface. In your case, IPoo.
This is preferable to using a generic type, in your case, because you are using a comment base class.
以下结合了其他几个答案的一些最佳方面以及一种技术,使
Cat
的关键方面具有所需RadioactivePoo< 的
Excrement
属性/code> 类型,但如果我们只知道我们有一个AnimalBase
而不是专门的Cat
,则能够将其仅返回为Poo
。调用者不需要使用泛型,即使它们存在于实现中,也不需要调用不同名称的函数来获取特殊的
Poo
。中间类
AnimalWithSpecializations
仅用于密封Excrement
属性,通过非公共SpecialPoo
属性将其连接到派生类AnimalWithSpecialPoo< ;TPoo>
具有派生返回类型的Excrement
属性。如果
Cat
是唯一一个其Poo
在任何方面都是特殊的动物,或者我们不希望Excrement
的类型成为主要定义Cat
的特征,可以在层次结构中跳过中间泛型类,以便Cat
直接从AnimalWithSpecializations
派生,但如果有多个不同的动物,其主要特征是它们的Poo
在某些方面是特殊的,将“样板文件”分离为中间类有助于保持Cat
类本身相当干净,尽管几个额外的虚拟函数调用的成本。示例代码显示大多数预期操作“按预期”工作。
The following combines some of the best aspects of several other answers as well as a technique to allow the key aspect of
Cat
having anExcrement
property of the requiredRadioactivePoo
type, but being able to return that as merelyPoo
if we only know we've got anAnimalBase
rather than specifically aCat
.Callers aren't required to use generics, even though they are present in the implementations, nor to call a differently-named function to get special
Poo
.The intermediate class
AnimalWithSpecialisations
serves only to seal theExcrement
property, connecting it via a non-publicSpecialPoo
property to the derived classAnimalWithSpecialPoo<TPoo>
which has anExcrement
property of a derived return type.If
Cat
is the only animal whosePoo
is special in any way, or we don't want the type ofExcrement
to be the main defining feature of aCat
, the intermediate generic class could be skipped in the hierarchy, so thatCat
derives directly fromAnimalWithSpecialisations
, but if there are several different animals whose primary characteristic is that theirPoo
is special in some way, separating out the "boilerplate" into intermediate classes helps to keep theCat
class itself fairly clean, albeit at the cost of a couple of extra virtual function calls.The example code shows that most of the expected operations work "as expected".
供参考。 这在 Scala 中很容易实现。
这是一个您可以使用的实例: http://www.scalakata.com/50d0d6e7e4b0a825d655e832
FYI. This is implemented quite easily in Scala.
Here is a live example you can play with: http://www.scalakata.com/50d0d6e7e4b0a825d655e832
好吧,实际上可以返回一个与继承的返回类型不同的具体类型(即使对于静态方法),这要归功于
dynamic
:我在为我的公司编写的 API 包装器中实现了这一点。 如果您计划开发 API,这有可能提高某些用例中的可用性和开发体验。 不过,使用
dynamic
会对性能产生影响,所以尽量避免使用。Well, it is actually possible to return a concrete Type which varies from the inherited return Type (even for static methods), thanks to
dynamic
:I implemented this in an API wrapper I wrote for my company. If you plan to develop an API, this has potential to improve the usability and dev experience in some use cases. Nevertheless, the usage of
dynamic
has an impact to the performance, so try to avoid it.