C#支持多重继承吗?
我和一位同事就多重继承发生了一些争论。我说不支持,他说支持。所以我想还是去问问网上那些有头脑的人吧。
A colleague and I are having a bit of an argument over multiple inheritance. I'm saying it's not supported and he's saying it is. So, I thought that I'd ask the brainy bunch on the net.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
抱歉,您不能从多个类继承。您可以使用接口或一个类和接口的组合,其中接口应遵循签名中的类名称。
可能的继承方式:
这是不合法的:
Sorry, you cannot inherit from multiple classes. You may use interfaces or a combination of one class and interface(s), where interface(s) should follow the class name in the signature.
Possible ways to inherit:
This is not legal:
不,请改用接口! ^.^
Nope, use interfaces instead! ^.^
C# 不支持多重继承。
但是,如果您想从两个来源“继承”行为,为什么不使用以下组合:
有一个基本但重要的 OOP 原则:“优先考虑组合而不是继承”。
您可以创建这样的类:
正如您所看到的,依赖项(接口的实际实现)是通过构造函数注入的。您的类不知道每个类是如何实现的,但它知道如何使用它们。因此,这里涉及的类之间是松散耦合的,但使用能力相同。
如今的趋势表明,继承已经“过时”了。
Multiple inheritance is not supported in C#.
But if you want to "inherit" behavior from two sources why not use the combination of:
There is a basic but important OOP principle that says: "Favor composition over inheritance".
You can create a class like this:
As you can see the dependecies (actual implementations of the interfaces) are injected via the constructor. You class does not know how each class is implemented but it knows how to use them. Hence, a loose coupling between the classes involved here but the same power of usage.
Today's trends show that inheritance is kind of "out of fashion".
C# 3.5 或更低版本不支持多重继承,但 C# 4.0 可以通过使用 Dynamics 来做到这一点。
C# 3.5 or below does not support the multiple inheritance, but C# 4.0 could do this by using, as I remembered, Dynamics.
在 C# 3.5 之前,您无法执行多重继承。我不知道它在 4.0 上的表现如何,因为我还没有看过它,但 @tbischel 发布了一个我需要阅读的链接。
C# 允许您通过接口进行“多重实现”,这与“多重继承”完全不同
所以,您不能这样做:
但是,您可以这样做:
HTH
You cannot do multiple inheritance in C# till 3.5. I dont know how it works out on 4.0 since I have not looked at it, but @tbischel has posted a link which I need to read.
C# allows you to do "multiple-implementations" via interfaces which is quite different to "multiple-inheritance"
So, you cannot do:
But, you can do:
HTH
C# 不支持类的多重继承,但允许您继承/实现任意数量的接口。
这是非法的
(B、C、D 和 E 都是类别)
这是合法的
(IB、IC、ID 和 IE 都是接口)
这是合法的
(B 是类,IC、ID 和 IE 是接口)
组合优于继承 是一种设计模式,即使在支持多重继承的语言中似乎也是受欢迎的。
C# does not support multiple inheritance of classes, but you are permitted to inherit/implement any number of interfaces.
This is illegal
(B, C, D & E are all classes)
This is legal
(IB, IC, ID & IE are all interfaces)
This is legal
(B is a class, IC, ID & IE are interfaces)
Composition over inheritance is a design pattern that seems to be favorable even in languages that support multiple inheritance.
实际上,这取决于您对继承的定义:
这不是“继承”一词通常的含义,但这样定义也并非完全没有道理。
Actually, it depends on your definition of inheritance:
This is not what is usually meant by the term "inheritance", but it is also not entirely unreasonable to define it this way.
与 Java(C# 间接派生自 Java)一样,C# 不支持多重继承。
也就是说,类data(成员变量和属性)只能从单个父基类继承。另一方面,类行为(成员方法)可以从多个父基接口继承。
一些专家,尤其是 Bertrand Meyer(被一些人认为是面向对象的鼻祖之一)编程),认为这使 C#(和 Java 以及所有其他语言)失去了成为“真正的”面向对象语言的资格。
Like Java (which is what C# was indirectly derived from), C# does not support multiple inhertance.
Which is to say that class data (member variables and properties) can only be inherited from a single parent base class. Class behavior (member methods), on the other hand, can be inherited from multiple parent base interfaces.
Some experts, notably Bertrand Meyer (considered by some to be one of the fathers of object-oreiented programming), think that this disqualifies C# (and Java, and all the rest) from being a "true" object-oriented language.
您可能想更进一步讨论设计模式 - 您可以找出为什么他愿意费心尝试从 C# 中的多个类继承(如果他可以的话)
You may want to take your argument a step further and talk about design patterns - and you can find out why he'd want to bother trying to inherit from multiple classes in c# if he even could
模拟多重继承模式
http://www.codeproject.com/KB/architecture/smip.aspx
Simulated Multiple Inheritance Pattern
http://www.codeproject.com/KB/architecture/smip.aspx
多重继承允许程序员创建结合多个类及其相应层次结构的各个方面的类。对于前。 C++ 允许从多个类继承
在 C# 中,类只允许从单个父类继承,这称为单继承。但是您可以使用接口或一个类和接口的组合,其中接口应该在签名中跟上类名。
例如:
您可以像下面这样继承:
class NewClass : X, Y { }
在上面的代码中,类“NewClass”是从多个接口创建的。
类 NewClass : FirstClass, X { }
在上面的代码中,类“NewClass”是从接口 X 和类“FirstClass”创建的。
Multiple inheritance allows programmers to create classes that combine aspects of multiple classes and their corresponding hierarchies. For ex. the C++ allows you to inherit from more than one class
In C#, the classes are only allowed to inherit from a single parent class, which is called single inheritance. But you can use interfaces or a combination of one class and interface(s), where interface(s) should be followed by class name in the signature.
Ex:
You can inherit like the following:
class NewClass : X, Y { }
In the above code, the class "NewClass" is created from multiple interfaces.
class NewClass : FirstClass, X { }
In the above code, the class "NewClass" is created from interface X and class "FirstClass".
一般来说,您做不到。
考虑这些接口和类:
您可以继承多个接口:
但是您不能继承多个类:
In generally, you can’t do it.
Consider these interfaces and classes:
You can inherit multiple interfaces:
But you can’t inherit multiple classes:
您不能一次继承多个类。但有一个选项可以通过界面的帮助来做到这一点。请参阅下面的代码,
您可以按如下方式调用它们
You can't inherit multiple classes at a time. But there is an options to do that by the help of interface. See below code
you can call them as below
不允许的话,就用接口来实现。
为什么会这样?
答案如下:它允许编译器做出非常合理且合理的决定,该决定始终与继承的内容和继承的位置保持一致,以便当您执行此操作时铸造,你总是确切地知道你正在处理哪个实现。
It does not allow it, use interface to achieve it.
Why it is so?
Here is the answer: It's allowed the compiler to make a very reasoned and rational decision that was always going to consistent about what was inherited and where it was inherited from so that when you did casting and you always know exactly which implementation you were dealing with.
C# 不支持内置的多重继承。
要为不支持的语言添加多重继承,可以使用 孪生设计模式
C# does not support multiple inheritance built in.
For adding multiple inheritance to languages that does not support it, You can use twin design pattern
作为对所建议内容的附加建议,提供类似于多重继承的功能的另一种巧妙方法是实现多个接口,但然后在这些接口上提供扩展方法。这称为 混入。这不是一个真正的解决方案,但它有时可以处理提示您想要执行多重继承的问题。
As an additional suggestion to what has been suggested, another clever way to provide functionality similar to multiple inheritance is implement multiple interfaces BUT then to provide extension methods on these interfaces. This is called mixins. It's not a real solution but it sometimes handles the issues that would prompt you to want to perform multiple inheritance.
我最近以某种方式达到了相同的心态,将两个类继承到一个类中,并最终出现在这个页面上(即使我知道得更好),并且希望保留对我在这种情况下发现的完美解决方案的引用,而不强制执行接口
我的这个问题的解决方案是将您的数据分成有意义的类:
稍后在您的代码中,您可以像这样使用它:
包括两个部分,基础和基础部分。 仅地址
基址:
仅地址:
I recently somehow got to the same mindset, inheriting two classes into a class and ended up on this page (even though i know better) and would like to keep reference to the solution i found perfect in this scenario, without enforcing implementation of interfaces
My solution to this problem would be splitting up your data into classes that make sense:
Later on in your code, you could use it like this:
Include Both Parts, Base & Address
Base Only:
Address Only: