是否可以指定“Derivative(of T)”的返回类型? VB.NET 中的 MustOverride 子程序?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
立即想到两个选项:
第一:
这将使所有派生类都具有 OrderItemBase 类型的属性(不是 OrderItemBase 的派生类)
第二:
请注意,这会更改类层次结构,以便所有订单都没有单一基类。您必须将任何适用于
OrderBase(Of TItem)
类的方法设为泛型。Two options come to mind immediately:
First:
which will make all derived classes have a property of type OrderItemBase (not a derived class of OrderItemBase)
Second:
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.