C#应该有多重继承吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/191691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Should C# have multiple inheritance?
提问by Richard Dorman
I have come across numerous arguments against the inclusion of multiple inheritance in C#, some of which include (philosophical arguments aside):
我遇到了许多反对在 C# 中包含多重继承的论点,其中一些包括(撇开哲学论点):
- Multiple inheritance is too complicated and often ambiguous
- It is unnecessary because interfaces provide something similar
- Composition is a good substitute where interfaces are inappropriate
- 多重继承太复杂,而且常常模棱两可
- 这是不必要的,因为接口提供了类似的东西
- 当接口不合适时,组合是一个很好的替代品
I come from a C++ background and miss the power and elegance of multiple inheritance. Although it is not suited to all software designs there are situations where it is difficult to deny it's utility over interfaces, composition and similar OO techniques.
我来自 C++ 背景,想念多重继承的力量和优雅。尽管它并不适合所有软件设计,但在某些情况下很难否认它在接口、组合和类似的面向对象技术上的实用性。
Is the exclusion of multiple inheritance saying that developers are not smart enough to use them wisely and are incapable of addressing the complexities when they arise?
排除多重继承是否意味着开发人员不够聪明,无法明智地使用它们,并且无法解决出现的复杂性?
I personally would welcome the introduction of multiple inheritance into C# (perhaps C##).
我个人欢迎将多重继承引入 C#(也许是 C##)。
Addendum: I would be interested to know from the responses who comes from a single (or procedural background) versus a multiple inheritance background. I have often found that developers who have no experience with multiple inheritance will often default to the multiple-inheritance-is-unnecessary argument simply because they do not have any experience with the paradigm.
附录:我有兴趣从来自单一(或程序背景)与多重继承背景的回复中了解。我经常发现没有多重继承经验的开发人员通常会默认多重继承是不必要的,因为他们没有任何范式经验。
回答by Marc Gravell
I've never missed it once, not ever. Yes, it [MI] gets complicated, and yes, interfaces do a similar job in many ways - but that isn't the biggest point: in the general sense, it simply isn't needed most of the time. Even single inheritance is overused in many cases.
我从来没有错过过一次,从来没有。是的,它 [MI] 变得复杂,是的,接口在很多方面都做着类似的工作——但这不是最重要的一点:一般来说,大多数时候根本不需要它。在许多情况下,甚至单继承也被过度使用。
回答by Bob King
I think it would over-complicate things without providing enough ROI. We already see people butcher .NET code with too-deep inheritance trees. I can just imagine the atrocities if people had the power to do multiple inheritance.
我认为如果不提供足够的投资回报率,它会使事情变得过于复杂。我们已经看到人们用太深的继承树屠杀 .NET 代码。我可以想象如果人们有权力进行多重继承会发生怎样的暴行。
I won't deny that it has potential, but I just don't see enough benefit.
我不会否认它具有潜力,但我只是没有看到足够的好处。
回答by David Arno
C# supports single inheritance, interfaces and extension methods. Between them, they provide just about everything that multiple inheritance provides, without the headaches that multiple inheritance brings.
C# 支持单继承、接口和扩展方法。在它们之间,它们几乎提供了多重继承所提供的一切,而没有多重继承带来的麻烦。
回答by Kibbee
I would argue against multiple inheritance simply for the reason you state. Developers will misuse it. I've seen enough problems with every class inheriting from a utility class, just so you can call a function from every class without needing to type so much, to know that multiple inheritance would lead to bad code in many situations. The same thing could be said about GoTo, which is one of the reasons it's use is so frowned upon. I think that multiple inheritance does have some good uses, just like GoTo, In an ideal world, where they were both only used when appropriately, there would be no problems. However, the world is not ideal, so we must protect bad programmers from themselves.
我会因为你陈述的原因反对多重继承。开发人员会滥用它。我已经看到从实用程序类继承的每个类都有足够多的问题,这样您就可以从每个类调用函数而无需键入太多内容,要知道多重继承在许多情况下会导致错误代码。关于 GoTo 也可以说同样的话,这是它的使用如此令人不悦的原因之一。我认为多重继承确实有一些很好的用途,就像GoTo一样,在理想的世界中,它们都只在适当的时候使用,不会有问题。然而,世界并不理想,所以我们必须保护糟糕的程序员远离他们自己。
回答by coder1
While there are certainly instances where it can be useful, I have found that most of the time when I think I need it, I really don't.
虽然在某些情况下它可能很有用,但我发现大多数时候当我认为我需要它时,我真的不需要。
回答by Scott Dorman
I have been working with C# since it was first available as an alpha/beta release and have never missed multiple inheritance. MI is nice for some things but there are almost always other ways to achieve the same result (some of which actually end up being simpler or creating an easier to understand implementation).
自从 C# 作为 alpha/beta 版本首次可用以来,我一直在使用它,并且从未错过多重继承。MI 对某些事情很好,但几乎总是有其他方法可以实现相同的结果(其中一些实际上最终更简单或创建更易于理解的实现)。
回答by Chris Cudmore
Prefer aggregation over inheritance!
更喜欢聚合而不是继承!
class foo : bar, baz
is often better handled with
通常更好地处理
class foo : Ibarrable, Ibazzable
{
...
public Bar TheBar{ set }
public Baz TheBaz{ set }
public void BarFunction()
{
TheBar.doSomething();
}
public Thing BazFunction( object param )
{
return TheBaz.doSomethingComplex(param);
}
}
This way you can swap in and out different implementations of IBarrable and IBazzable to create multiple versions of the App without having to write yet another class.
通过这种方式,您可以换入和换出 IBarrable 和 IBazzable 的不同实现来创建应用程序的多个版本,而无需编写另一个类。
Dependency injection can help with this a lot.
依赖注入对此有很大帮助。
回答by Nemanja Trifunovic
Multiple inheritance in general can be useful and many OO languages implement it one way or another (C++, Eiffel, CLOS, Python...). Is it essential? No. Is it nice to have? Yes.
多重继承通常很有用,许多 OO 语言以一种或另一种方式实现它(C++、Eiffel、CLOS、Python...)。它是必不可少的吗?不。有吗?是的。
回答by Mecki
Update
I challenge everyone who votes me down to show me any example of multiple inheritance that I can't easily port to a language with single inheritance. Unless anyone can show any such sample, I claim it does not exist. I have ported tons of C++ code (MH) to Java (no-MH) and that was never a problem, no matter how much MH the C++ code used.
更新
我挑战所有投票给我的人,向我展示我无法轻松移植到具有单继承的语言的任何多重继承示例。除非有人可以展示任何此类样本,否则我声称它不存在。我已经将大量 C++ 代码 (MH) 移植到 Java(无 MH),这从来都不是问题,无论 C++ 代码使用了多少 MH。
Nobody could ever prove so far that multiple inheritance has any advantageover other techniques you mentioned in your post (using interfaces and delegates I can get exactly the same result without much code or overhead), while it has a couple of well known disadvantages (diamond problembeing the most annoying ones).
到目前为止,没有人能够证明多重继承比您在帖子中提到的其他技术有任何优势(使用接口和委托,我可以在没有太多代码或开销的情况下获得完全相同的结果),但它有几个众所周知的缺点(菱形问题是最烦人的)。
Actually multiple inheritance is usually abused. If you use OO design to somehow model the real world into classes, you will never get to the point where multiple inheritance makes actually sense. Can you provide a useful example for multiple inheritance? Most of the examples I've seen so far are actually "wrong". They make something a subclass, that is in fact just an extra property and thus actually an interface.
实际上多重继承通常被滥用。如果您使用 OO 设计以某种方式将现实世界建模为类,您将永远不会达到多重继承真正有意义的地步。你能为多重继承提供一个有用的例子吗?到目前为止,我看到的大多数示例实际上都是“错误的”。他们使某些东西成为子类,这实际上只是一个额外的属性,因此实际上是一个接口。
Take a look at Sather. It is a programming language, where interfaces do have multiple inheritance, as why not (it can't create a diamond problem), however classes that are no interfaces have no inheritance whatsoever. They can only implement interfaces and they can "include" other objects, which makes these other objects a fixed part of them, but that is not the same as inheritance, it's rather a form of delegation (method calls "inherited" by including objects are in fact just forwarded to instances of these objects encapsulated in your object). I think this concept is pretty interesting and it shows you can have a complete clean OO language without any implementation inheritance at all.
看看萨瑟。它是一种编程语言,其中接口确实具有多重继承,为什么不这样(它不能创建菱形问题),但是没有接口的类没有任何继承。它们只能实现接口,它们可以“包含”其他对象,这使得这些其他对象成为它们的固定部分,但这与继承不同,它是一种委托形式(通过包含对象来“继承”的方法调用是实际上只是转发到封装在您的对象中的这些对象的实例)。我认为这个概念非常有趣,它表明您可以拥有一个完全干净的面向对象语言,而无需任何实现继承。
回答by Lou Franco
A colleague wrote this blog about how to get something like multiple inheritance in C# with Dynamic Compilation:
一位同事写了这篇关于如何使用动态编译在 C# 中获得类似多重继承的博客: