vb.net VB .Net 中是否可以进行多重继承?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/351242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is multiple inheritance possible in VB .Net?
提问by Steve Duitsman
Is multiple inheritance possible in VB .Net? If so, what is the syntax?
VB .Net 中是否可以进行多重继承?如果是这样,语法是什么?
回答by Harper Shelby
Short answer: No
简短回答:否
Slightly longer answer: Yes, if you inherit multiple interfaces, and a single base class. Since this is usually the reason for MI (you want to implement multiple interfaces), it's usually enough. However, in those rare instances where "real" MI is useful, .NET prevents you from doing it.
稍微长一点的答案:是的,如果您继承多个接口和一个基类。由于这通常是 MI 的原因(您要实现多个接口),因此通常就足够了。但是,在“真正的”MI 有用的极少数情况下,.NET 会阻止您这样做。
回答by Joel Coehoorn
It's possible in a restricted manner in VB.Net in the same way that it is in C#: via Interfaces. Since an interface works out to essentially a pure-abstract base class, you can inherit from as many of those as you need and from one real class.
它可以在 VB.Net 中以与在 C# 中相同的方式受限方式实现:通过接口。由于接口本质上是一个纯抽象基类,因此您可以根据需要从多个基类和一个真正的类中继承。
回答by VoteCoffee
Likely what you want to do is really composition or aggregation ( see here for design pattern). Maybe you're defining a behavior. You can always implement an interface SomeInterface in the base class, have a member of type SomeInterface (which lets it be any class that implements SomeInterface and can hence have the code does the implementing), in the members constructor pass a reference to the base class that owns it if necessary (if doing so, try to add another interface to define the callbacks, the base class will implement it and the subclass will have it as the member variable type). Use calls to the member class to implement SomeInterface. This way the code is implemented in another class, which makes it easy to maintain, but you're not doing multiple inheritance.
可能您想要做的是真正的组合或聚合(请参阅此处了解设计模式)。也许你正在定义一种行为。你总是可以在基类中实现接口 SomeInterface,有一个 SomeInterface 类型的成员(它允许它是任何实现 SomeInterface 的类,因此可以让代码执行实现),在成员构造函数中传递对基类的引用必要时拥有它(如果这样做,尝试添加另一个接口来定义回调,基类将实现它,子类将其作为成员变量类型)。使用对成员类的调用来实现 SomeInterface。通过这种方式,代码在另一个类中实现,这使得维护起来很容易,但您没有进行多重继承。
The idea behind composition is that an engine is not a car but a car has an engine. The car needs an engine, but doesn't need to know how a whole engine unit works, just how to interface with it. So the engine should not inherit from car. But having the car implement the engine is silly. So the car gets an engine as a member of the whole car, but as an object. The car has an engine as part of its composition.
组合背后的想法是,引擎不是汽车,但汽车有引擎。汽车需要一个引擎,但不需要知道整个引擎单元是如何工作的,只需要知道如何与之交互。所以引擎不应该继承自汽车。但是让汽车安装引擎是愚蠢的。因此,汽车将发动机作为整个汽车的一部分,但作为一个对象。这辆车有一个发动机作为其组成的一部分。
It sounds like what you are doing is more of a behavior, like a duck object that has a quack behavior, but rubber ducks are ducks but do not quack but squeak. So they differ from mallard objects, but both have many other duck features in common. So you want to have a quack interface that each implements differently. But many ducks will quack for this interface, so you don't want to have to write quack for each one. That's where you use composition to implement the quack behavior interface.
听起来您所做的更像是一种行为,就像鸭子对象具有嘎嘎声一样,但橡皮鸭子是鸭子,但不会嘎嘎叫,而是吱吱作响。所以它们不同于野鸭物体,但两者都有许多其他共同的鸭子特征。所以你想有一个江湖接口,每个接口都实现不同。但是很多鸭子会为这个接口嘎嘎叫,所以你不想每一个都写嘎嘎。这就是您使用组合来实现 quack 行为接口的地方。
回答by user1954025
As far as I know VB.net does not support multiple inheritance in general but you can reach a kind of multiple inheritance by working with interfaces (using “Implements” instead of “Inherits”):
据我所知,VB.net 通常不支持多重继承,但您可以通过使用接口(使用“Implements”而不是“Inherits”)来实现一种多重继承:
Public Class ClassName
Implements BaseInterface1, BaseInterface2
End Class
That works fine for classes but I'd like to have an interface inheriting some base interfaces. Something like that:
这适用于类,但我希望有一个继承一些基本接口的接口。类似的东西:
Public Interface InterfaceName
Implements BaseInterface1, BaseInterface2
End Interface
But the “Implements” keyword is not allowed for interfaces (what makes sense, of course). I tried to use a kind of abstract class which I know from Java:
但是接口不允许使用“Implements”关键字(当然,这是有道理的)。我尝试使用一种我从 Java 中知道的抽象类:
Public MustInherit Class InterfaceName
Implements BaseInterface1, BaseInterface2
End Class
But now I need to implement the defined methods from BaseInterface1 and BaseInterface2 within the InterfaceName class. But as InterfaceName should be an interface, too, I don't want to have to implement these methods within that class.
但是现在我需要在 InterfaceName 类中实现 BaseInterface1 和 BaseInterface2 中定义的方法。但是由于 InterfaceName 也应该是一个接口,所以我不想在该类中实现这些方法。
In C# you can do that quite easy:
在 C# 中,你可以很容易地做到这一点:
public interface InterfaceName: BaseInterface1, BaseInterface2 {}