简体   繁体   English

确实可以使用接口实现多重继承吗

[英]Does really interfaces can be use to achieve multiple inheritance

I have this confusion for long time. 我很困惑。 Many people says we can achieve multiple inheritance by interfaces in languages like C# or Java which does not support it like C++ does. 许多人说,我们可以通过C#或Java等不支持C ++的语言的接口实现多重继承。 But my understanding of inheritance and interface says no. 但是我对继承和接口的理解没有。 Because interfaces are contracts to validate an implementation which has nothing to do with behavior. 因为接口是用于验证与行为无关的实现的合同。 Interface defines what something can do (not what something is). 接口定义什么可以做(而不是什么)。 But inheritance is inheriting behavior and/or property from parents (like a child is getting some genetic behavior from his parent - which is inheritance). 但是继承是从父母那里继承行为和/或财产(就像孩子从父母那里得到一些遗传行为一样,这就是继承)。 Now the child is learning a skills say painting and cooking and the interface (a certificate or contract) acts as a validation that the child is having such skills (that is what the child can do other than what he got from his parents - and that's not inheritance) 现在,孩子正在学习一种技能,例如绘画和烹饪,并且界面(证书或合同)可以证明该孩子具有这些技能(这是他可以做的事情,而不是他从父母那里得到的-这就是不继承)

So am I understanding it wrong? 所以我理解错了吗? And if not then why it is saying that we can achieve multiple inheritance using interfaces? 如果不是,那为什么要说我们可以使用接口实现多重继承呢?

Interfaces give you multiple inheritance of a type, but not behaviour. 接口为您提供了类型的多个继承,但没有行为。 A class implementing List and Map is a "ListMap", but the implementation has nothing (necessarily) to do with any existing List or Map implementation. 实现List and Map类是“ ListMap”,但该实现与(现有)与任何现有的ListMap实现无关。

Of course using composition (which should be favored anyways), you could easily create a ListMap which delegates the calls accordingly to its list and map properties, while providing some presumably useful function that would combine their respective data. 当然,使用组合(无论如何都应该受到欢迎),您可以轻松创建一个ListMap ,将调用相应地委派给它的listmap属性,同时提供一些可能有用的函数来组合各自的数据。

With Java 8 interfaces are allowed default methods, so inheritance of behaviour is now also possible. 使用Java 8接口可以使用默认方法,因此现在也可以继承行为。

In Java you can create an interface for example Animal and an abstract class Bird . 在Java中,您可以创建接口,例如Animal和抽象类Bird

then you can have a class MockingBird which extends the behavior of Bird and implements the actions of an Animal . 然后您可以拥有一个MockingBird类, MockingBird扩展了Bird的行为并实现了Animal的动作。

However, you can then address MockingBird as an Animal or as a Bird because it "inherits" from both. 但是,您可以将MockingBird作为AnimalBird MockingBird ,因为它是两者的“继承”

No, interfaces cannot be used to achieve multiple inheritance 不可以,接口不能用于实现多重继承

Not at all in Java, in C#, we can get closer. 在Java中根本不是,在C#中,我们可以更加紧密地联系。

I studied this matter when I wanted to implement an observer, and ended up in Robert Martin's blog: http://blog.cleancoder.com/uncle-bob/2015/01/08/InterfaceConsideredHarmful.html 当我想实现观察者的时候,我研究了这个问题,并最终在罗伯特·马丁(Robert Martin)的博客中: http : //blog.cleancoder.com/uncle-bob/2015/01/08/InterfaceConsideredHarmful.html

After reading this post I realized he's talking about Java, but C# supports extension methods which allow you to attach behaviour on interfaces so I tried to make my implementation on some IObservable interface but obviously failed, even if I can attach behaviour in such interfaces extension methods I'm still not allowed for attaching state on them, if some day microsoft decides to implement extension properties then this combination (Interface + Extension methods + Extension properties) could be sufficient to truly simulate some useful multiple inheritance. 阅读这篇文章后,我意识到他正在谈论Java,但是C#支持扩展方法,该方法允许您在接口上附加行为,因此我尝试在IObservable接口上实现我的实现,但即使我可以在此类接口扩展方法中附加行为,也显然失败了。我仍然不允许在其上附加状态,如果有一天Microsoft决定实现扩展属性,则此组合(接口+扩展方法+扩展属性)可能足以真正模拟一些有用的多重继承。

For now, we are stuck with duplicating the code, or the delegation code in all our observers as stated in the blog. 就目前而言,我们一直坚持在博客中声明的所有观察者中复制代码或委派代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM