为什么 Java 或 C# 中不允许多重继承? [英] Why is Multiple Inheritance not allowed in Java or C#?

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

问题描述

我知道 Java 和 C# 中不允许多重继承.很多书只是说,不允许多重继承.但是可以通过接口来实现.没有讨论为什么不允许这样做.谁能告诉我为什么不允许这样做?

解决方案

简短的回答是:因为语言设计者决定不这样做.

基本上,.NET 和 Java 设计者似乎都不允许多重继承,因为他们认为添加 MI给语言增加了太多的复杂性,同时提供的好处太少强>.

为了获得更有趣和更深入的阅读,网上有一些文章,其中包含对一些语言设计者的采访.例如,对于 .NET,Chris Brumme(曾在 MS 从事 CLR 的工作)解释了他们决定不这样做的原因:

<块引用>

  1. 实际上不同的语言对 MI作品.例如,冲突是如何产生的已解决以及是否重复碱基合并或冗余.在我们可以之前在 CLR 中实现 MI,我们必须做对所有语言的调查,图找出共同的概念,并决定如何在一个语言中立的方式.我们也会必须决定 MI 是否属于CLS 以及这意味着什么不想要这个概念的语言(例如,大概是 VB.NET).的当然,这就是我们从事的行业作为公共语言运行时,但我们还没来得及为 MI 做这件事

  2. 真正适合 MI 的地方数量其实相当多小的.在许多情况下,多个接口继承可以搞定完成了.在其他情况下,您可以能够使用封装和代表团.如果我们要添加一个略有不同的构造,例如mixins,那实际上会更多吗强大吗?

  3. 多实现继承为执行.这种复杂性影响选角、布局、调度、现场访问、序列化、身份比较,可验证性,反射,泛型,可能还有还有很多其他地方.

您可以在此处阅读全文.

对于Java,您可以阅读这篇文章:

<块引用>

省略多个的原因从 Java 语言继承大多源于简单、对象导向的、熟悉的"目标.作为简单的语言,Java 的创造者想要一种大多数开发人员都喜欢的语言无需大量的就可以掌握训练.为此,他们致力于使语言与 C++ 类似可能(熟悉)无需携带C++ 不必要的复杂性(简单的).

在设计师看来,多个继承会导致更多的问题和混乱比它解决.所以他们剪了语言的多重继承(就像他们削减运营商超载).设计师的广泛C++ 经验告诉他们多重继承不值得头痛.

I know that multiple inheritance is not allowed in Java and C#. Many books just say, multiple inheritance is not allowed. But it can be implemented by using interfaces. Nothing is discussed about why it is not allowed. Can anybody tell me precisely why it is not allowed?

解决方案

The short answer is: because the language designers decided not to.

Basically, it seemed that both the .NET and Java designers did not allow multiple inheritance because they reasoned that adding MI added too much complexity to the languages while providing too little benefit.

For a more fun and in-depth read, there are some articles available on the web with interviews of some of the language designers. For example, for .NET, Chris Brumme (who worked at MS on the CLR) has explained the reasons why they decided not to:

  1. Different languages actually have different expectations for how MI works. For example, how conflicts are resolved and whether duplicate bases are merged or redundant. Before we can implement MI in the CLR, we have to do a survey of all the languages, figure out the common concepts, and decide how to express them in a language-neutral manner. We would also have to decide whether MI belongs in the CLS and what this would mean for languages that don't want this concept (presumably VB.NET, for example). Of course, that's the business we are in as a common language runtime, but we haven't got around to doing it for MI yet.

  2. The number of places where MI is truly appropriate is actually quite small. In many cases, multiple interface inheritance can get the job done instead. In other cases, you may be able to use encapsulation and delegation. If we were to add a slightly different construct, like mixins, would that actually be more powerful?

  3. Multiple implementation inheritance injects a lot of complexity into the implementation. This complexity impacts casting, layout, dispatch, field access, serialization, identity comparisons, verifiability, reflection, generics, and probably lots of other places.

You can read the full article here.

For Java, you can read this article:

The reasons for omitting multiple inheritance from the Java language mostly stem from the "simple, object oriented, and familiar" goal. As a simple language, Java's creators wanted a language that most developers could grasp without extensive training. To that end, they worked to make the language as similar to C++ as possible (familiar) without carrying over C++'s unnecessary complexity (simple).

In the designers' opinion, multiple inheritance causes more problems and confusion than it solves. So they cut multiple inheritance from the language (just as they cut operator overloading). The designers' extensive C++ experience taught them that multiple inheritance just wasn't worth the headache.

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

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