Enum 和 Constant Java 类有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24812265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between Enum and Constant Java class?
提问by Surya
I am getting confused regarding Enum.
我对Enum.
Where and how to use Enum?
在哪里以及如何使用Enum?
If I will use Enuminstead of constantthen what could be the benefit?
如果我将使用Enum而不是constant那么会有什么好处?
Could someone please explain to me?
有人可以向我解释一下吗?
回答by Mind Peace
The primary advantage is type safety. With a set of constants, any value of the same intrinsic type could be used, introducing errors. With an enum only the applicable values can be used.
主要优点是类型安全。使用一组常量,可以使用相同内在类型的任何值,从而引入错误。对于枚举,只能使用适用的值。
回答by ihgann
An Enum class is used to specify a range of constants that will be frequently used within an application.
Enum 类用于指定将在应用程序中经常使用的常量范围。
From http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html:
从http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html:
You should use enum types any time you need to represent a fixed set of constants. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.
任何时候需要表示一组固定的常量时,都应该使用枚举类型。常见示例包括罗盘方向(北、南、东和西的值)和一周中的几天。
A constant class (assuming this is what you are talking about), is really specified for one solid object that you wouldn't change. Technically, an Enum class is a constant class with more definitions, but an advantage to an Enum class is some pre-defined functionsthat you would have to define in your own constant class. However, you should also note that this may be overkill for small, singleton examples.
一个常量类(假设这就是你所说的),实际上是为一个你不会改变的实体对象指定的。从技术上讲,Enum 类是具有更多定义的常量类,但 Enum 类的一个优点是您必须在自己的常量类中定义一些预定义的函数。但是,您还应该注意,这对于小型的单例示例可能有点过头了。
Additionally, Enum classes are type-safe, whereas static fields aren't. Compile time error checking is now possible, versus the run-time potential errors that will occur with a constant class. This furthermore improves readability, because instead of having errors where an index of a list of constants is unavailable. This is all well explained in this post, by the current best answer.
此外,枚举类是类型安全的,而静态字段不是。现在可以进行编译时错误检查,而不是使用常量类会发生的运行时潜在错误。这进一步提高了可读性,因为不会在常量列表的索引不可用时出现错误。这在这篇文章中得到了很好的解释,目前的最佳答案。
回答by Ronald Duck
As of Java 8.0 the keyword "constant" does not exist.
从 Java 8.0 开始,关键字“constant”不存在。
If you mean "const": This keyword is in the known keywords in Java (from the beginning until today - 8.0), but it has no use and any compiler shouldn't compile if it find's this keyword.
如果您的意思是“const”:此关键字在 Java 中的已知关键字中(从一开始直到今天 - 8.0),但它没有用,任何编译器如果找到 this 关键字,则不应编译。
Enumerations: Use them when you have specific logical values and don't want to handle them in String, like:
枚举:当您有特定的逻辑值并且不想在 String 中处理它们时使用它们,例如:
public enum AcceptableColours {
BLACK,
WHITE,
GREY
}
It's much safer during development time to match enum values
在开发期间匹配枚举值更安全
AcceptableColours currentColour = object.getColour;
if (currentColour == acceptableColours.BLACK) {
// do something
} else if (currentColour == acceptableColours.WHITE) {
// do something different
}
than to match string values
比匹配字符串值
AcceptableColours currentColor = object.getColour;
if (currentColor.equals("black")) {
// do something
} else if (currentColor.equals("white")) {
// do something different
}
... imagine huge applications with much more variations on enum values (like banking software, insurance software, ...), where it's much easier to misspell some string values, and voilà, happy debugging...
...想象一下在枚举值上有更多变化的大型应用程序(如银行软件、保险软件等),其中一些字符串值更容易拼错,瞧,调试愉快...

