VB .Net中是否可以进行多重继承?

时间:2022-06-02 22:39:41

Is multiple inheritance possible in VB .Net? If so, what is the syntax?

VB .Net中是否可以进行多重继承?如果是这样,语法是什么?

4 个解决方案

#1


18  

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会阻止您这样做。

#2


4  

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#中的方式相同:通过接口。由于接口本质上是一个纯抽象的基类,因此您可以根据需要从一个真正的类继承。

#3


1  

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.

这听起来像你正在做的更多的是一种行为,就像一个有庸医行为的鸭子对象,但橡皮鸭是鸭子,但不要嘎嘎而是发出吱吱声。因此它们与野鸭物体不同,但两者都有许多共同的鸭子特征。所以你想拥有一个每个实现不同的庸医界面。但是很多鸭子会为这个界面嘎嘎叫,所以你不想为每个人写嘎嘎。这就是你使用组合来实现庸医行为界面的地方。

#4


1  

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 {}

#1


18  

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会阻止您这样做。

#2


4  

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#中的方式相同:通过接口。由于接口本质上是一个纯抽象的基类,因此您可以根据需要从一个真正的类继承。

#3


1  

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.

这听起来像你正在做的更多的是一种行为,就像一个有庸医行为的鸭子对象,但橡皮鸭是鸭子,但不要嘎嘎而是发出吱吱声。因此它们与野鸭物体不同,但两者都有许多共同的鸭子特征。所以你想拥有一个每个实现不同的庸医界面。但是很多鸭子会为这个界面嘎嘎叫,所以你不想为每个人写嘎嘎。这就是你使用组合来实现庸医行为界面的地方。

#4


1  

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 {}