为什么用枚举代替常量?

时间:2021-07-17 11:50:19

I have a scenario in which I have Player types ARCHER,WARRIOR, and sorcerer. What should I use in player class? A String variable for type or a Enum? Why enum and why Constant Strings. Please help with reasons.

我有一个场景,我有球员类型的弓箭手,战士,和魔术师。我应该在玩家类中使用什么?类型或枚举的字符串变量?为什么是enum,为什么是常量字符串。请帮忙的原因。

5 个解决方案

#1


81  

Suppose you use constant strings (or int values - the same goes for them):

假设您使用常量字符串(或int值,同样适用于它们):

// Constants for player types
public static final String ARCHER = "Archer";
public static final String WARRIOR = "Warrior";

// Constants for genders
public static final String MALE = "Male";
public static final String FEMALE = "Female";

then you end up not really knowing the type of your data - leading to potentially incorrect code:

然后你最终不知道你的数据类型——导致潜在的错误代码:

String playerType = Constants.MALE;

If you use enums, that would end up as:

如果你使用enums,那最终会是:

// Compile-time error - incompatible types!
PlayerType playerType = Gender.MALE;

Likewise, enums give a restricted set of values:

同样,enums也提供一组受限制的价值观:

String playerType = "Fred"; // Hang on, that's not one we know about...

vs

vs

PlayerType playerType = "Fred"; // Nope, that doesn't work. Bang!

Additionally, enums in Java can have more information associated with them, and can also have behaviour. Much better all round.

此外,Java中的枚举可以有更多与它们相关的信息,也可以有行为。更好的四周。

#2


9  

Enums limit you to the required set of inputs whereas even if you use constant strings you still can use other String not part of your logic.

枚举限制您使用所需的输入集,而即使您使用常量字符串,您仍然可以使用其他字符串,而不是逻辑的一部分。

This helps you to not make a mistake, to enter something out of the domain, while entering data and also improves the program readability.

这可以帮助您在输入数据时避免错误地输入域之外的内容,并提高程序的可读性。

Additionally you can always use your enums as a String if you desire. Here is a reference.

此外,如果您愿意,您可以随时使用您的枚举作为字符串。这是一个参考。

#3


5  

Besides not letting you to provide an incorrect value, there is yet another feature of enums that may seem minor, but in my opinion is quite important. Modern IDEs can automatically suggest values for enums, while there is no way to reliably infer the possible values of a string constant (Intellij IDEA does the latter, but only for JDK classes and popular libraries). This is especially helpful when you are exploring a new API.

除了不允许您提供不正确的值之外,枚举还有另一个看似次要的特性,但在我看来非常重要。现代ide可以自动地为枚举提供值,而无法可靠地推断字符串常量的可能值(Intellij IDEA做的是后者,但只针对JDK类和流行的库)。当您正在探索一个新的API时,这一点特别有用。

#4


0  

I would advice you to use enums only if you really need enumerated constants, or some additional functionality common for all items.

我建议您仅当您确实需要枚举常量或所有项的一些附加功能时才使用枚举。

That's of course depending on the type of application you are writing and what versions and devices you want to support.

当然,这取决于您所编写的应用程序的类型以及您希望支持的版本和设备。

The reason is, enums add overhead because they allocate instances of their items. You can notice, that there are minimum enums in android platform, and almost all constants are final static ints (like View.GONE and stuff like that)

原因是,enums增加了开销,因为它们分配条目的实例。您可以注意到,android平台中有最小枚举,几乎所有常量都是最终的静态ints(如View)。消失了,诸如此类的东西

#5


0  

Enum is better to use for type safety. Wrong values cannot be entered. But enum in android takes so much memory, you should use intdef instead. Refer to this answer for Example and explanation:-

IntDef/StringDef Example

枚举最好用于类型安全。错误的值不能输入。但是android中的enum占用太多内存,您应该使用intdef来代替。请参考此答案以获得示例和解释:- IntDef/StringDef示例

You can also check android source code it is replacing enums with IntDef/StringDef wherever possible. Ex. View.VISIBLE.

你也可以检查android的源代码,它正在尽可能地用IntDef/StringDef来替代enums。View.VISIBLE。

#1


81  

Suppose you use constant strings (or int values - the same goes for them):

假设您使用常量字符串(或int值,同样适用于它们):

// Constants for player types
public static final String ARCHER = "Archer";
public static final String WARRIOR = "Warrior";

// Constants for genders
public static final String MALE = "Male";
public static final String FEMALE = "Female";

then you end up not really knowing the type of your data - leading to potentially incorrect code:

然后你最终不知道你的数据类型——导致潜在的错误代码:

String playerType = Constants.MALE;

If you use enums, that would end up as:

如果你使用enums,那最终会是:

// Compile-time error - incompatible types!
PlayerType playerType = Gender.MALE;

Likewise, enums give a restricted set of values:

同样,enums也提供一组受限制的价值观:

String playerType = "Fred"; // Hang on, that's not one we know about...

vs

vs

PlayerType playerType = "Fred"; // Nope, that doesn't work. Bang!

Additionally, enums in Java can have more information associated with them, and can also have behaviour. Much better all round.

此外,Java中的枚举可以有更多与它们相关的信息,也可以有行为。更好的四周。

#2


9  

Enums limit you to the required set of inputs whereas even if you use constant strings you still can use other String not part of your logic.

枚举限制您使用所需的输入集,而即使您使用常量字符串,您仍然可以使用其他字符串,而不是逻辑的一部分。

This helps you to not make a mistake, to enter something out of the domain, while entering data and also improves the program readability.

这可以帮助您在输入数据时避免错误地输入域之外的内容,并提高程序的可读性。

Additionally you can always use your enums as a String if you desire. Here is a reference.

此外,如果您愿意,您可以随时使用您的枚举作为字符串。这是一个参考。

#3


5  

Besides not letting you to provide an incorrect value, there is yet another feature of enums that may seem minor, but in my opinion is quite important. Modern IDEs can automatically suggest values for enums, while there is no way to reliably infer the possible values of a string constant (Intellij IDEA does the latter, but only for JDK classes and popular libraries). This is especially helpful when you are exploring a new API.

除了不允许您提供不正确的值之外,枚举还有另一个看似次要的特性,但在我看来非常重要。现代ide可以自动地为枚举提供值,而无法可靠地推断字符串常量的可能值(Intellij IDEA做的是后者,但只针对JDK类和流行的库)。当您正在探索一个新的API时,这一点特别有用。

#4


0  

I would advice you to use enums only if you really need enumerated constants, or some additional functionality common for all items.

我建议您仅当您确实需要枚举常量或所有项的一些附加功能时才使用枚举。

That's of course depending on the type of application you are writing and what versions and devices you want to support.

当然,这取决于您所编写的应用程序的类型以及您希望支持的版本和设备。

The reason is, enums add overhead because they allocate instances of their items. You can notice, that there are minimum enums in android platform, and almost all constants are final static ints (like View.GONE and stuff like that)

原因是,enums增加了开销,因为它们分配条目的实例。您可以注意到,android平台中有最小枚举,几乎所有常量都是最终的静态ints(如View)。消失了,诸如此类的东西

#5


0  

Enum is better to use for type safety. Wrong values cannot be entered. But enum in android takes so much memory, you should use intdef instead. Refer to this answer for Example and explanation:-

IntDef/StringDef Example

枚举最好用于类型安全。错误的值不能输入。但是android中的enum占用太多内存,您应该使用intdef来代替。请参考此答案以获得示例和解释:- IntDef/StringDef示例

You can also check android source code it is replacing enums with IntDef/StringDef wherever possible. Ex. View.VISIBLE.

你也可以检查android的源代码,它正在尽可能地用IntDef/StringDef来替代enums。View.VISIBLE。