我需要一个小的静态数据树,其中每个成员可以拥有一些变量或包含子成员
我想我会尝试像这样构建它
Public Class nBase
Public Shared Name As String
Public Shared Description As String
Public Class nFruit
Inherits nBase
Shared Sub New()
Name = "Fruit" : Description = "Grows on trees"
End Sub
Public Class nApple
Inherits nFruit
Shared Sub New()
Name = "Apple" : Description = "I'm an apple!"
End Sub
End Class
End Class
Public Class nVegetable
Inherits nBase
Shared Sub New()
Name = "Vegetable" : Description = "does the tomato belong here?"
End Sub
End Class
End Class
然后是实际应用的一个例子
Public Sub makestock()
Dim Stock As New Dictionary(Of Type, Integer)
Stock.Add(GetType(nBase.nFruit), 2)
Stock.Add(GetType(nBase.nFruit.nApple), 5)
Stock.Add(GetType(nBase.nVegetable), 2)
End Sub
所以在这里我宣布我的库存包括5个苹果,2个蔬菜和2个水果(注意:苹果的数量不计算在水果中,因为在我的上下文中它只是意味着水果的类型不是&# 39; t定义为总共7个水果,其中5个苹果和2个未定义)
现在也许是一个想要了解股票代码描述的GUI功能,所以
Dim description = Activator.CreateInstance(Stock.Keys(0)).Description
大。这就是我需要的#34;数据库"。但是,正如您可能已经注意到的那样,它在使用方面非常笨重。这就是我认为理想的东西:
Stock.add(nBase.nFruit, 2)
Dim description = Stock.Keys(0).Description
(是的,我知道这不起作用,想象代码)。有什么方法可以让我更接近理想吗?就这样,整个事情就不那么痛苦了......
答案 0 :(得分:1)
您可以通过更改列表来存储对象而不是类型,每个对象包含实例属性。然后,您可以在基类型上拥有共享属性,以显示各种派生类型的一些单例实例:
Public Class nBase
Public Property Name As String
Public Property Description As String
Public Shared ReadOnly Property Fruit As New nFruit()
Public Shared ReadOnly Property Apple As New nFruit.nApple()
Public Shared ReadOnly Property Vegetable As New nVegetable()
Public Class nFruit
Inherits nBase
Public Sub New()
Name = "Fruit" : Description = "Grows on trees"
End Sub
Public Class nApple
Inherits nFruit
Public Sub New()
Name = "Apple" : Description = "I'm an apple!"
End Sub
End Class
End Class
Public Class nVegetable
Inherits nBase
Public Sub New()
Name = "Vegetable" : Description = "does the tomato belong here?"
End Sub
End Class
End Class
' ...
Dim stock As New Dictionary(Of nBase, Integer)()
stock.Add(nBase.Fruit, 2)
Dim description = Stock.Keys(0).Description
然而,值得指出的是,确实没有必要嵌套它们。无论嵌套类如何,继承都有效。例如,你可以让它们都不是嵌套的:
Public Class nBase
Public Property Name As String
Public Property Description As String
Public ReadOnly Property Fruit As New nFruit()
Public ReadOnly Property Apple As New nApple()
Public ReadOnly Property Vegetable As New nVegetable()
End Class
Public Class nFruit
Inherits nBase
Public Sub New()
Name = "Fruit" : Description = "Grows on trees"
End Sub
End Class
Public Class nApple
Inherits nFruit
Public Sub New()
Name = "Apple" : Description = "I'm an apple!"
End Sub
End Class
Public Class nVegetable
Inherits nBase
Public Sub New()
Name = "Vegetable" : Description = "does the tomato belong here?"
End Sub
End Class
根据您的评论,您可以执行以下操作:
Public Class nBase
Public Property Name As String
Public Property Description As String
Public Shared ReadOnly Property Fruit As New nFruit()
Public Shared ReadOnly Property Vegetable As New nVegetable()
Public Class nFruit
Inherits nBase
Public Sub New()
Name = "Fruit" : Description = "Grows on trees"
End Sub
Public ReadOnly Property Apple As New nApple()
Public Class nApple
Inherits nFruit
Public Sub New()
Name = "Apple" : Description = "I'm an apple!"
End Sub
End Class
End Class
Public Class nVegetable
Inherits nBase
Public Sub New()
Name = "Vegetable" : Description = "does the tomato belong here?"
End Sub
End Class
End Class
' ...
Dim stock As New Dictionary(Of nBase, Integer)()
stock.Add(nBase.Fruit, 2)
stock.Add(nBase.Fruit.Apple, 5)
stock.Add(nBase.Vegetable, 2)
Dim description As String = stock.Keys(0).Description