dotnet不支持多重继承。但多个接口支持?

时间:2010-08-09 05:34:30

标签: c# .net inheritance multiple-inheritance

  

可能重复:
  Multiple Inheritance in C#

dotnet不支持多重继承。但是多个接口支持。为什么存在这种行为。 任何具体原因??

6 个答案:

答案 0 :(得分:4)

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

  

实际上有不同的语言   如何MI的不同期望   作品。例如,冲突是怎样的   解决了是否重复的基础   合并或冗余。在我们能做之前   在CLR中实现MI,我们必须这样做   图中所有语言的调查   出于共同的概念,并做出决定   如何表达它们   语言中立的方式。我们也会   必须决定MI是否属于   CLS以及这意味着什么   不想要这个概念的语言   (大概是VB.NET,例如)。的   当然,这就是我们所处的业务   作为公共语言运行库,但我们   没有为MI做这件事   爱好。

     

MI真正的地方数量   适当的实际上很小。   在很多情况下,多个接口   继承可以完成工作   代替。在其他情况下,你可能是   能够使用封装和   代表团。如果我们要添加一个   略有不同的构造,如   mixins,实际上会更多   强大?

     

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

答案 1 :(得分:2)

是继承意味着将属性从一个类对象获取到另一个类对象..

这里的接口概念我们根本没有得到任何属性,而是我们在类中实现接口的未实现方法...

所以继承和接口完全相反......

所以最后java只支持多重继承的语法,不支持多重继承的实现....

继承就像借记,接口就像信用......但是接口在服务器端编程等其他概念中有其自身的重要性......

答案 2 :(得分:1)

通常,多重继承会产生比解决的问题更多的问题。考虑如何解决虚方法调用。如果一个类没有定义一个方法但是它的父母都这样做怎么办?哪一个应该执行?

然而,实现多个接口没有这样的问题。如果两个接口定义了相同的方法并且您实际上尝试实现它们,那么您的代码甚至不会编译(尽管我不确定您是否可以显式实现它们并满足编译器要求)。

答案 3 :(得分:1)

  

因为接口没有   实施细节,他们只知道   对象可以执行什么操作。   多重继承很难   有两种不同   实现是为了找到的   两者中具有相同签名的方法   基类。但是在接口的情况下   两个界面都可以定义一个共同点   具有相同签名的方法但它们   没有在界面上实现   级别,它们仅由实现   实现它们的对象或类型   接口。这里虽然有   两个不同的接口定义两个   具有相同签名的方法,   对象提供了共同点   实现既满足又满足   两个接口中的方法。所以   之间没有歧义   实现,两种方法都有   通常的实施因此你可以   如果有的话有多重继承   接口

答案 4 :(得分:0)

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

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

还有很多其他原因导致多重继承不好。

答案 5 :(得分: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...
相关问题