2010-08-09 97 views

回答

1

一般来说,多继承会产生比解决问题更多的问题。想想如何解决虚拟方法调用。如果一个班级没有定义一个方法,但是它的父母都这么做,会怎么样?哪一个应该执行?但是,实现多个接口不存在这样的问题。如果两个接口定义了相同的方法并且实际尝试实现它们,那么代码甚至不会编译(虽然我不确定是否可以明确地实现它们并满足编译器的要求)。

+0

你可以明确地定义他们俩 – 2010-08-09 06:02:44

+0

在实现两个接口定义的方法时,不应该有任何编译器错误。 – Dienekes 2010-12-26 11:52:43

0

具体类的多重继承存在的危险是存储和虚拟方法查找必须在给定类的两个或多个父母之间进行协调。特别棘手的是当有共同的祖先。但接口只定义了类应该是什么样子,而不是如何实现它,让类看起来像很多不同的东西,比让它变得很多不同的东西容易得多。两个接口可能需要一个int Foo()方法,而一个实现类可以安全地使用这两个接口并实现Foo(),而不会引起基准Foo()覆盖的问题等。

另一个原因是构造器链很难用多重继承进行管理。但接口不指定构造函数,所以这个问题完全被回避了。

为什么多继承是坏的肯定有很多其他的原因。

4

您可以使用接口模拟多重继承。如果允许多个类继承,则会导致Diamond problem
至于原因,不支持多重继承,我建议你阅读Why doesn't C# support multiple inheritance?

不同的语言,事实上我对如何MI 作品 不同的期望。例如,如何解决冲突 以及重复的基地 是合并还是冗余。之前,我们可以 在CLR实现MI,我们要做的 所有语言,人物 与众不同的概念进行了调查,并决定 如何表达他们的 语言中立的方式。我们还要 必须决定MI是否属于 CLS以及这对于 语言如何不想要这个概念 (大概是VB.NET)。 当然,这是我们在 作为一个公共语言运行时的业务,但我们 还没有得到这样做MI 呢。

MI确实是 适当的地方的数量实际上非常小。 在许多情况下,多个接口 继承可以代替完成作业 。在其他情况下,您可能是 能够使用封装和 代表团。如果我们要添加一个 稍微不同的构造,比如 mixins,那实际上会更强大吗? 强大吗?

多个实现继承 为 实现注入了很多复杂性。这种复杂性 影响铸造,布局,调度, 字段访问,序列化,标识 比较,可验证性, 反射,泛型,并可能 许多其他地方。

1

因为接口不与 实现细节,他们只知道 一个对象可以做什么操作。 当 存在两种不同时,多重继承是困难的 在 基类中具有相同签名的 方法的实现。但在接口 的情况下,两个接口可以定义公共 方法用相同的签名,但他们 没有在接口 水平,它们仅由 实现的对象或类型实现实现既 的接口。这里虽然有 两个不同的接口定义了两个具有相同签名的 方法,但 对象提供的通用 实现满足两个接口中的 方法。所以 有 实现之间没有歧义,这两种方法都有 共同实施,因此,你可以 有多重继承在 接口的情况下。

0
java supports syntactical multiple inheritance....java does not supports implementation of multiple inheritance... 

some people says java supports multiple inheritance through interfaces ...but its not correct here the explanation: 

inheritance :: 

getting the properties from one class object to another class object.. 

Class A{} 

Class B extends A {} 

here object of class A getting the properties(methods/functions/ & data members/class variables) 

why java does not supports multiple inheritance using classes: 

Class A{} Class B{} Class C extends A,B{} X--this statement causes error because Class A getting object of object class from both sides A and B... 

every java class by default extending Object of object class...and Object of object class is the root object means super class for all classes... 

but here Class c having two super classes of object...so giving error...means java does not support multiple inheritance by using classes.. 

is java supports multiple inheritance using Interfaces:: 

because of this interface concept only few of us saying that java supports multiple inheritance....but its wrong.. 

here the explanation:: 

interface A{} 

interface B{} 

interface C implements A , B{} 

(or) interface A{} 

interface B{} 

Class C implements A , B{} 

here its look like multiple inheritance but ..... 

inheritance means getting the properties from one class object to another class object.. 

here in interface concept we are not at all getting any properties rather we are implementing the unimplemented methods of interface in class... 

so inheritance and intefaces are quite opposite... 

so finally java supports only syntax of multiple inheritance does not supports implementation of multiple inheritance.... 

inheritance is like debit and interface is like credit....but interface has its own importance in other concepts like server side programming... 
2

是继承意味着越来越从一个类对象的属性到另一个类的对象..

在这里界面概念,我们不会在所有得到任何特性,而我们正在实施的接口的未实现的方法在课堂上...

所以继承和intefaces是完全相反......

所以最后的Java支持多重继承的只是语法不支持我多重继承的mplementation ....

继承就像借记卡和界面中,就像信贷....但界面有像服务器端编程等概念自身的重要性......