关于c#:dotnet不支持多重继承。

Multiple inheritance is not supported in dotnet. But multiple interface supports?

本问题已经有最佳答案,请猛点这里访问。

Possible Duplicate:
Multiple Inheritance in C#

dotnet中不支持多重继承。但多接口支持。为什么会有这种行为。有什么具体的原因吗??


可以使用接口模拟多个继承。如果允许使用多个类继承,则会导致菱形问题。由于不支持多个继承,我建议您阅读"为什么C不支持多个继承?"

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.

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?

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.


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

在接口概念中,我们根本不获取任何属性,而是在类中实现接口的未实现方法…

所以遗传和智力是完全相反的…

因此,Java最终支持多继承的语法不支持多继承的实现。

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


Because interfaces do not the
implementation details, they only know
what operations an object can do.
Multiple inheritance is difficult when
there are two different
implementations are found for the
method with same signature in both the
base classes. But in case of interface
both the interface may define a common
method with same signature but they
are not implemented at the interface
level, they are only implemented by
the object or type that implement both
the interfaces. Here though there are
two different interfaces defining two
methods with same signatures, the
object provides the common
implementation satisfying both the
methods in both the interfaces. So
there is no ambiguity between
implementations, both the methods have
common implementation hence you could
have multiple inheritance in case of
interfaces.


一般来说,多重继承产生的问题比它解决的问题要多。考虑如何解决虚拟方法调用。如果一个类没有定义一个方法,但它的两个父类都定义了该方法怎么办?哪一个应该执行?

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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...

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

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

多重继承当然还有很多其他的原因。