为什么C#不支持多重继承? [英] Why C# does not support Multiple Inheritance ?

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

问题描述

为什么C#不支持多重继承?如果它是唯一的Diamond问题,那么他们为什么不能做一些像显式重写的事情。

在多接口概念中我们有明确的实现,那么为什么Microsoft没有实现类似于多继承的东西。

Why C# does not support Multiple Inheritance ? If it the only Diamond problem, then why can't they do something like explicit overriding.
In multiple interface concept we have explicit implementation, then why Microsoft not implemented something similar to that in multiple inheritance.

推荐答案

它只支持所谓的多重继承的弱形式:每个接口或类在其继承中可以包含任意数量的类型,但列出的第一个类型应该是一个类,所有其他类型只能是接口。换句话说,这是接口的多重继承;并且每个类可以实现任意数量的接口,但只有一个基类(除了没有基类的公共根 System.Object )。



为什么?类的多重继承是一个非常矛盾的问题,并导致潜在的设计问题。没有这种继承也会导致潜在的设计问题。这种争议是OOP技术的固有问题之一。这不仅仅是钻石问题;并且使用显式覆盖无法解决此问题。事实上,压倒一直是明确的,那么呢?如果您认为有解决方案,您很可能误解了这个问题。通过使用虚拟基础和非虚拟基础之间的选择,实际上在C ++中解决了该问题。但是这个功能本身就是一个固有的问题。我可以解释一下原因。因为这个选择本身(在使用虚拟和非虚拟基础之间)不能成为后期绑定的问题。一旦你选择了某个类作为虚拟基础,它将保留为你可能决定稍后添加的所有其他派生类的虚拟。



不幸的是,它无法解释它在快速答案中,但问题非常明显;当你面临严重的OOD问题时,你将能够感受到它们。也许,如果我必须涵盖这些问题的所有方面,并且有多个方面要克服它们(我的意思是,用不同的编程语言做出的替代决策,而不是应用程序问题),我可以写一本关于这个主题的书。



-SA
It supports only so called "weak form of multiple inheritance": each interface or class can have any number of types in its inheritance least, but the first type listed should be a class, and all other types can only be interfaces. In other words, this is multiple inheritance for interfaces; and each class can implement any number of interfaces but has exactly one base class (except the common root System.Object which does not have a base class).

Why? Multiple inheritance for classes is a very contradictory issue and causes inherent potential design problems. The absence of such inheritance also causes inherent potential design problems. This controversy is one of the inherent problems of OOP technology. This is not just the diamond problem; and this problem cannot be solved with explicit overriding. In fact, overriding is already explicit, so what? If you think there is a solution, you most likely misunderstood this problem. The problem is actually resolved in C++ by using the choice between virtual base and non-virtual. But this feature itself presents an inherent problem. I can explain why. Because this choice itself (between using virtual and non-virtual base) cannot be a matter of late binding. Once you have chosen some class to be a virtual base, it will remain virtual for all other derived classes you may decide to add later.

Unfortunately, it's impossible to explain it in a Quick Answer, but the problems are quite apparent; you will be able to feel them when you face serious OOD problems. Probably, if I had to cover all the aspects of such problems and multiple approached to overcome them (I mean, alternative decisions made in different programming languages, not application problems), I could write a whole book on the topic.

—SA


查看以下链接



http://blogs.msdn.com/b/csharpfaq/archive/ 2004/03/07 / 85562.aspx [ ^ ]


多重继承创建紧密耦合,使维护变得困难。



因此,在任何严肃的应用程序中,您都希望避免使用多重继承,即使它在C ++中可用。



对于可扩展性和重用目的,最好是主对象包含子对象。因此,不是从A,BC和D继承,而是对象A具有B,C和D类型的子对象。



此时您可以轻松更改派生的子对象并支持很多组合。



如果有意义,你甚至可以在运行时更改子对象或者决定在创建时使用哪个子对象。这提供了很大的灵活性。



多重继承的一个问题是,一旦你经常使用它,你会发现你经常有错误的父类组合并且你有很多重构来调整类层次结构或者创建类似的类,这些类只有少数一个或两个基类之间的区别。



假设你继承了来自3个类,每个类有3个子类。如果您需要所有可能性,您将拥有4 x 4 x 4种可能性,为父母提供64种组合。



另一方面,如果这些是子对象,那么你有一个单独的类,每个子对象有4个可能的类。



父类也有很多功能,你可能需要保持70左右如果您更改一个虚拟功能,则需要80个班级。



在另一种情况下,您只需要更改4个班级......
Multiple inheritance create tight coupling which make maintenance hard.

Thus in any serious application, you want to avoid using multiple inheritance even when it is available like in C++.

For extensibility and reuse purpose, it is better to that your main object contains sub-object. Thus instead of inheriting from say A, B C and D, your object A has sub-object of type B, C and D.

At that point you can easily change a sub-object for a derived one and support a lot of combination.

When it make sense, you might even be able to change a sub-object at run time or decide which sub-object to uses at creation time. This give a lot of flexibility.

One problem with multiple inheritance is that once you use it a lot, you will find that you often have the wrong combination of parent classes and you have a lot of refactoring to adjust the class hierarchy or create similar classes that differs only from one or 2 bases classes among a few one.

Say that you inherited from 3 classes and each of those have 3 subclasses. If you need all possibilities, you will have 4 x 4 x 4 possibilities for parents giving 64 combinations.

On the other hand, if those are sub-object, then you have a single class with 4 possible classes for each sub-object.

Also the parent class would have a lot of unctions and you might have to maintain around 70 to 80 classes if you change one virtual functions.

In the other case, you'll have to change only 4 classes...


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

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