是否可以在Visual Basic中创建对象数组?
我正在制作一个战斗系统,每当战斗开始时,我都想能够从数组中随机选择一个Monster对象。
如果有可能,有人可以告诉我如何将Public Spider as New Monster(50, 20, 5)
存储到数组中吗?
谢谢你。
怪物职业:
Public Class Monster
Private hp As Integer
Private xp As Integer
Private dmg As Integer
Sub New(ByVal hitpoints As Integer, ByVal exp As Integer, ByVal damage As Integer)
hp = hitpoints
xp = exp
dmg = damage
End Sub
End Class
表单类别:
Imports Monster
Public Class Form
Public Spider As New Monster(50, 20, 5)
End Class
最佳答案
List(Of T)为此非常有用。
Private Monsters As New List(Of Monster)
'later add them into this collection
Monsters.Add(New Monster(50, 20, 5))
关于arrays - 如何创建对象数组Visual Basic,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16724938/