VB .Net 中可以实现多重继承吗?

发布于 2024-07-10 13:37:26 字数 40 浏览 6 评论 0原文

VB .Net 中可以实现多重继承吗? 如果是这样,语法是什么?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

茶底世界 2024-07-17 13:37:27

简短的回答:否

稍微长一点的回答:是的,如果您继承多个接口和一个基类。 由于这通常是 MI 的原因(您想要实现多个接口),因此通常就足够了。 然而,在“真正的”MI 有用的极少数情况下,.NET 会阻止您这样做。

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.

情绪 2024-07-17 13:37:27

在 VB.Net 中可以采用与 C# 中相同的受限方式:通过接口。 由于接口本质上是一个纯抽象基类,因此您可以根据需要从多个基类继承,也可以从一个真正的类继承。

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.

脸赞 2024-07-17 13:37:27

据我所知,VB.net 一般不支持多重继承,但您可以通过使用接口来实现某种多重继承(使用“实现”而不是“继承”):

Public Class ClassName
    Implements BaseInterface1, BaseInterface2

End Class

这对于类来说效果很好,但我希望有一个继承一些基接口的接口。 类似这样的事情:

Public Interface InterfaceName
    Implements BaseInterface1, BaseInterface2

End Interface

但是接口不允许使用“Implements”关键字(当然,这是有道理的)。 我尝试使用一种我从 Java 中了解到的抽象类:

Public MustInherit Class InterfaceName
    Implements BaseInterface1, BaseInterface2

End Class

但现在我需要在 InterfaceName 类中实现 BaseInterface1 和 BaseInterface2 中定义的方法。 但由于 InterfaceName 也应该是一个接口,所以我不想在该类中实现这些方法。

在 C# 中,你可以很容易地做到这一点:

public interface InterfaceName: BaseInterface1, BaseInterface2 {}

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”):

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:

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.

In C# you can do that quite easy:

public interface InterfaceName: BaseInterface1, BaseInterface2 {}
鹿港小镇 2024-07-17 13:37:27

可能您真正想做的是组合或聚合(请参阅 此处了解设计模式)。 也许您正在定义一种行为。 您始终可以在基类中实现接口 SomeInterface,拥有 SomeInterface 类型的成员(这使得它可以是实现 SomeInterface 的任何类,因此可以让代码执行实现),在成员构造函数中传递对基类的引用如有必要,拥有它(如果这样做,请尝试添加另一个接口来定义回调,基类将实现它,子类将其作为成员变量类型)。 使用对成员类的调用来实现 SomeInterface。 这样代码就在另一个类中实现,这使得维护变得容易,但你并没有进行多重继承。

组合背后的想法是,发动机不是汽车,而是汽车有发动机。 汽车需要发动机,但不需要知道整个发动机单元如何工作,只需要知道如何与其连接。 所以引擎不应该继承自汽车。 但让汽车实现引擎是愚蠢的。 因此,汽车将发动机作为整个汽车的一部分,但作为一个对象。 汽车的组成部分有发动机。

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文