是否可以指定“Derivative(of T)”的返回类型? VB.NET 中的 MustOverride 子程序?

发布于 2024-08-30 05:46:12 字数 764 浏览 10 评论 0原文

VB.NET 2008 .NET 3.5

我有两个 MustInherit (部分)基类。我们将一个类称为 OrderBase,将另一个类称为 OrderItemBase。

特定类型的订单和订单项将从这些类继承。我们将这些称为 WebOrder(继承自 OrderBase)和 WebOrderItem(继承自 OrderItemBase)。

现在,在宏伟的计划中,WebOrder 是一个包含 WebOrderItem 的复合类,如下所示:

Public Class WebOrder
    Inherits OrderBase

    Public Property OrderItem() as WebOrderItem
    End Property

End Class

Public Class WebOrderItem
    Inherits OrderItemBase
End Class

为了确保从 OrderBase 派生的任何类都具有 OrderItem 属性,我想在 OrderBase 类中执行类似的操作

Public MustInherit Class OrderBase
    Public MustOverride Property OrderItem() as Derivative(Of OrderItemBase)
End Class

:换句话说,我希望派生类强制包含一个返回 OrderItemBase 派生类的属性。

这是可能的,还是我应该使用完全不同的方法?

VB.NET 2008 .NET 3.5

I have two base classes that are MustInherit (partial). Let's call one class OrderBase and the other OrderItemBase.

A specific type of order and order item would inherit from these classes. Let's call these WebOrder (inherits from OrderBase) and WebOrderItem (inherits from OrderItemBase).

Now, in the grand scheme of things WebOrder is a composite class containing a WebOrderItem, like so:

Public Class WebOrder
    Inherits OrderBase

    Public Property OrderItem() as WebOrderItem
    End Property

End Class

Public Class WebOrderItem
    Inherits OrderItemBase
End Class

In order to make sure any class that derives from OrderBase has the OrderItem property, I would like to do something like this in the OrderBase class:

Public MustInherit Class OrderBase
    Public MustOverride Property OrderItem() as Derivative(Of OrderItemBase)
End Class

In other words, I want the derived class to be forced to contain a property that returns a derivative of OrderItemBase.

Is this possible, or should I be using an entirely different approach?

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

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

发布评论

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

评论(1

云淡风轻 2024-09-06 05:46:12

立即想到两个选项:

第一:

Public MustInherit Class OrderBase
    Public MustOverride Property OrderItem As OrderItemBase
End Class

Public Class WebOrder
    Inherits OrderBase

    Public Overrides Property OrderItem As OrderItemBase
End Class

这将使所有派生类都具有 OrderItemBase 类型的属性(不是 OrderItemBase 的派生类)

第二:

Public MustInherit Class OrderBase(Of TItem As OrderItemBase)
    Public MustOverride Property OrderItem As TItem
End Class

Public Class WebOrder
    Inherits OrderBase(Of WebOrder)

    Public Overrides Property OrderItem As WebOrder
End Class

请注意,这会更改类层次结构,以便所有订单都没有单一基类。您必须将任何适用于 OrderBase(Of TItem) 类的方法设为泛型。

Two options come to mind immediately:

First:

Public MustInherit Class OrderBase
    Public MustOverride Property OrderItem As OrderItemBase
End Class

Public Class WebOrder
    Inherits OrderBase

    Public Overrides Property OrderItem As OrderItemBase
End Class

which will make all derived classes have a property of type OrderItemBase (not a derived class of OrderItemBase)

Second:

Public MustInherit Class OrderBase(Of TItem As OrderItemBase)
    Public MustOverride Property OrderItem As TItem
End Class

Public Class WebOrder
    Inherits OrderBase(Of WebOrder)

    Public Overrides Property OrderItem As WebOrder
End Class

Note that this changes the class hierarchy such that there is no single base class for all your orders. You will have to make any method that works on the OrderBase(Of TItem) class generic.

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