为什么多重继承在C#中不可用 [英] Why Multiple Inheritance not available in C#

查看:92
本文介绍了为什么多重继承在C#中不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么多个inheritanc没有在C#中实现。

解决方案

检查一下: http://www.programmerinterview.com/index.php/general-miscellaneous/why-dont- c-and-java-support-multiple-inheritance / [ ^ ]


如果你做了搜索,你会发现很多在网上回答。



顺便说一下,CodeProject上已经提出了类似的问题并且有更完整的答案:为什么C#不支持多重继承? [ ^ ]



鉴于多重继承难以实现,并且主要影响虚拟表和强制转换的实现,并且众所周知多重继承会导致很多问题,因此很容易理解为什么C#不支持它。 br />


虽然C ++支持它,很多人会建议避免使用它(除了接口)。几乎在任何时候都可以使用多重继承,最好重新考虑设计并使用单继承和编写包装器函数,将它们转发到内部对象,以便在多重继承的情况下可以从一个额外的基类中获得。



这将减少耦合,它还允许改变类型子对象,这是多重继承所不可能的。





此外,每个潜在的功能都有一些潜在的好处,也有可能被使用。在某些情况下,它只会简化代码,而在其他情况下,它可以解决完全新问题。最后每个功能也可能存在潜在的问题。



因此选择哪个功能构成语言是PRO和CON之间的决定。



在多重继承的情况下,我们有很多CON:

- 难以实施。

- 很少使用。

- 高耦合。

- 维护成本高。

- 被滥用的可能性很高。

- In大多数情况下,替代解决方案更好(例如组合和实现多个接口)。

- 对整个系统性能的潜在影响(对于演员和类型系统更复杂的代码)。

- 使语言更复杂。

- 可以影响所有针对.NET的语言。

- ...


< blockquote>虚拟方法太复杂而无法正确实现。


I want to know that why multiple inheritanc not implemented in C#.

解决方案

Check this out: http://www.programmerinterview.com/index.php/general-miscellaneous/why-dont-c-and-java-support-multiple-inheritance/[^]


If you would have done a search you would have find a lot of answer on the web.

By the way, a similar question was already asked here on CodeProject and have more complete answers : Why C# does not support Multiple Inheritance ?[^]

Given that multiple inheritance is hard to implement and have majors impacts on the implementation of virtual tables and cast and that it was already well known that multiple inheritance cause a lot of problems, it is easy to understand why it was not supported in C#.

Although C++ support it, a lot of people would recommand to avoid using it (except from "interface"). Almost any time multiple inheritance could be used, it would be preferable to reconsider the design and use single inheritance and write wrapper fonctions that forward them to an internal object for function that would be available from an additionnal base class under multiple inheritance.

This will reduce coupling and it also allows to vary the type subobject which is not possible with multiple inheritance.


Also each potential feature has some potential benefits and also a potential of being used. In some cases, it only simplify code and in other it allows to solve completly new problems. Finally each feature can have potential problems too.

Thus choosing which feature make part of language is a decision between the PRO and CON of having it.

In the case of multiple inheritance, we have a lot of CONs:
- Hard to implement.
- Seldom used.
- High coupling.
- High maintenance cost.
- High chance of being misused.
- In most case, alternate solutions are better (for ex. composition and implementing multiple interfaces).
- Potential impact on the whole system performance (more complex code for cast and type system).
- Make the language more complex.
- Can affect all language that target to .NET.
- ...


Virtual methods are too complex to properly implement.


这篇关于为什么多重继承在C#中不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆