在VB.NET中使用继承,会给我们的编程带来很大的好处,现在我们就详细的看一个关于VB.NET继承的商业例子:每一个定货都有一个线路项,可能有产品线路项和服务线路项。这两种线路项是有些不同的。但是当我们在分开实现ProductLine和ServiceLine类的时候,我们就会发现其实它们有许多相同之处。如果分开编写这两个类的代码,不仅编程效率低,而且程序代码也难以维护,所以***的方法就是使用它们一些相同的代码。
#T#为了实现使用相同的代码,VB.NET继承就起了很大的作用了。使用VB.NET继承,我们可以创建一个LineItem类(父类),它包含了所有的共用代码。然后我们再创建ProductLine和ServiceLine子类,这两个类是由LineItem继承而来的。这样它们就可以自动地获得所有的共用代码了。假如LineItem类为:
Public Class LineItem
Private mintID As Integer
Private mstrItem As String
Private msngPrice As Single
Private mintQuantity As Integer
Public Property ID() As Integer
Get
Return mintID
End Get
Set
mintID = value
End Set
End Property
Public Property Item() As String
Get
Return mstrItem
End Get
Set
mstrItem = Value
End Set
End Property
Public Property Price() As Single
Get
Return msngPrice
End Get
Set
msngPrice = Value
End Set
End Property
Public Property Quantity() As Integer
Get
Return mintQuantity
End Get
Set
mintQuantity = Value
End Set
End Property
Public Function Amount() As Single
Return mintQuantity * msngPrice
End Function
End Class
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
这个类中有所有的共用代码以及一些基本的数据区域和用于计算项目价钱的方法。