简体   繁体   English

使用枚举或字符串表示对象类型的优缺点

[英]Benefits and drawbacks of using enum or string to represent object type

I have a winform application in vb.net and am developing a Rock Paper Scissor game. 我在vb.net中有一个winform应用程序,正在开发Rock Paper Scissor游戏。

I use enum in one place and string in another to represent some an weapon type as shown below: 我在一个地方使用枚举,在另一个地方使用字符串表示某种武器类型,如下所示:

ENUM example: ENUM示例:

    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

STRING example: STRING示例:

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

Can you tell me the advantages and disadvantages of each approach? 您能告诉我每种方法的优点和缺点吗? Im new to OOP and coding so I just want to what the issue is with this approach and what would be the better approach? 我是OOP和编码的新手,所以我只想了解这种方法的问题所在,哪种方法更好?

Any help would be greatly appreciated. 任何帮助将不胜感激。

Many Thanks, 非常感谢,

If you are going to use strings, you should at least use constants. 如果要使用字符串,则至少应使用常量。 Using string literals everywhere is just a bad idea because you can easily type something wrong. 在任何地方使用字符串文字都是一个坏主意,因为您可以轻松键入错误的内容。 By always using constants, at least you can avoid that issue. 通过始终使用常量,至少可以避免该问题。 Secondly, by using constants, it is easy to change the string value, if you ever need to, or to find all the places in the code where the constant is used: 其次,通过使用常量,可以很容易地更改字符串值(如果需要),也可以在代码中找到使用常量的所有位置:

Global Const RockWeapon As String = "Rock"
Global Const PaperWeapon As String = "Paper"
Global Const ScissorsWeapon As String = "Scissors"

However, the problem still exists that it is not at all obvious, when reading the code, what all of the possible values are. 但是,仍然存在一个问题,即在读取代码时,所有可能的值是什么都不是很明显。 This can be further alleviated by grouping all the string constants into a class so that it essentially works like an enum, like this: 可以通过将所有字符串常量分组到一个类中来进一步缓解该问题,使其本质上像枚举一样工作,如下所示:

Public Class Weapons
    Public Const RockWeapon As String = "Rock"
    Public Const PaperWeapon As String = "Paper"
    Public Const ScissorsWeapon As String = "Scissors"
End Class

Then, you could use the constants like this: 然后,您可以使用如下常量:

Public Sub pickWeapon(ByVal WeaponType As String)
    If WeaponType = Weapons.Rock Then
        pWeapon = New Rock()
    ElseIf WeaponType = Weapons.Paper Then
        pWeapon = New Paper()
    Else
        pWeapon = New Scissors()
    End If
End Sub

However, the code is still more confusing than it needs be, in that, when you call the pickWeapon method, all it tells you is that you need to pass it a string. 但是,该代码仍然比需要的代码更加混乱,因为当您调用pickWeapon方法时,它只告诉您需要将其传递给字符串。 So, to make it clear, you would need to add a comment that explains that you must pass one of the constants in the Weapons class. 因此,为了清楚起见,您需要添加一条注释,以说明您必须传递Weapons类中的常量之一。

It's always better, when possible, to have the code be self-documenting. 在可能的情况下,使代码具有自记录性总是更好。 When you ask for the enum, you know immediately what possible values the method expects, so there is no need to add a comment to that affect. 当您请求枚举时,您会立即知道该方法期望什么可能的值,因此无需对此影响添加注释。

Additionally, since everywhere you want to set or compare the value, you want to use the constant instead of the literal, it makes no difference to the rest of the code what that actual value is. 此外,由于到处都想设置或比较该值,因此您想使用常量而不是文字,因此它对其余代码的实际值没有影响。 Rock could equal "Rock", or "ROCK", or "Hey Look, I'm a String!". Rock可以等于“ Rock”,“ ROCK”或“ Hey Look,I'm String!”。 As long as everywhere in the code uses the constant, it will still work. 只要代码中的任何地方都使用该常量,它就仍然可以工作。 So, if that's the case, then there's really no advantage to having a string over just using an integer value in an enum. 因此,如果是这种情况,那么与仅在枚举中使用整数值相比,拥有字符串确实没有任何优势。

There are some times where a string constant does make more sense. 在某些情况下,字符串常量确实更有意义。 For instance, if you are saving the value to a database, and the value in the database is more useful as a human-readable value, it may make sense to use "Rock" instead of 0. But as long as you don't have any extenuating circumstances where you need it to be a string, I would definitely recommend sticking to using the enumeration for all of the above reasons. 例如,如果将值保存到数据库中,并且数据库中的值作为人类可读的值更有用,则可以使用“ Rock”而不是0。但是,只要您不这样做,在任何需要将其作为字符串的情况下,出于上述所有原因,我绝对建议您坚持使用枚举。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM