我在 vb.net 中有一个 winform 应用程序,并且正在开发一个石头剪刀布游戏。
我在一个地方使用枚举,在另一个地方使用字符串来表示某种武器类型,如下所示:
枚举示例:
Imports RockPaperScissors.Weapon
Public Class PlayerComputerRandom
Inherits Player
Private Enum weaponsList
Rock
Paper
Scissors
End Enum
Public Overloads Sub pickWeapon()
Dim randomChoice = New Random()
Dim CompChoice As Integer = randomChoice.Next(0, [Enum].GetValues(GetType(weaponsList)).Length)
If CompChoice = "0" Then
pWeapon = New Rock()
ElseIf CompChoice = "1" Then
pWeapon = New Paper()
Else
pWeapon = New Scissors()
End If
End Sub
End Class
字符串示例:
Public Class Player
Public pWeapon As Weapon
Public Sub pickWeapon(ByVal WeaponType As String)
If WeaponType = "Rock" Then
pWeapon = New Rock()
ElseIf WeaponType = "Paper" Then
pWeapon = New Paper()
Else
pWeapon = New Scissors()
End If
End Sub
End Class
你能告诉我每种方法的优缺点吗?我是 OOP 和编码的新手,所以我只想知道这种方法的问题是什么,更好的方法是什么?
任何帮助将不胜感激。
非常感谢,