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

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

问题描述

我知道,多重继承没有在Java和C#允许的。许多书只是说,多重继承是不允许的。但它可以通过使用接口来实现。为什么不允许任何讨论。谁能告诉我precisely为什么不允许?


解决方案

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

基本上,它似乎,无论是.NET和Java的设计者没有允许多重继承,因为他们的理由是,增加心肌梗死的添加太多的复杂性以语言,同时提供的好处太少

对于更多的乐趣和深入的阅读,还有一些在网络上可用的文章有一些语言设计者的采访。例如,对于.NET,克里斯Brumme(谁在MS的CLR工作)解释了原因,他们决定不:


  

      
  1. 不同的语言实际上如何MI不同的期望
      作品。例如,如何冲突
      解决是否重复基地
      合并或冗余。之前,我们可以
      在CLR实现MI,我们要做的
      所有语言的调查,人物
      出了共同的概念,并决定
      如何在一个前preSS他们
      语言无关的方式。我们还
      必须决定是否MI所属
      在CLS以及这将意味着
      不想要这个概念语言
      (presumably VB.NET,例如)。的
      当然,那是生意,我们在
      作为一种公共语言运行库,但我们
      还没有得到周围做它的MI
      然而。


  2.   
  3. 的地方MI是真正合适的数量其实是相当
      小。在许多情况下,多个
      接口继承可以得到这份工作
      代替完成。在其他情况下,你可能
      能够使用封装和
      代表团。如果我们要增加一个
      稍有不同的结构,如
      混入,将实际比较
      强大?


  4.   
  5. 多重实现继承注入了很多复杂的进
      实现。这种复杂性
      影响铸造,布局,调度,
      字段访问,序列化,身份
      比较,可验证性,
      反思,仿制药,而且可能
      很多其他地方。


  6.   

你可以在这里阅读全文。

有关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天全站免登陆