Java 中的枚举。优点?
在 Java 中使枚举类似于类,而不是像 C/C++ 中的常量集合有哪些优点?
What are some advantages of making enum in Java similar to a class, rather than just a collection of constants as in C/C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您可以免费在编译时检查有效值。使用
并不确保
只接受 0 或 1 作为参数值。使用枚举,这是有保证的。此外,这会导致更多的自记录代码,因为您可以使用代码完成来查看所有枚举值。
You get free compile time checking of valid values. Using
does not ensure
will only accept 0 or 1 as a parameter value. Using an enum, that is guaranteed. Moreover, this leads to more self documenting code, because you can use code completion to see all enum values.
类型安全是原因之一。
另一个我发现更重要的是,您可以将元数据附加到Java 中的枚举值。例如,您可以使用枚举来定义 Web 服务的合法操作集,然后附加请求类型和数据类的元数据:
Type safety is one reason.
Another, that I find more important, is that you can attach metadata to enum values in Java. For example, you could use an enum to define the set of legal operations for a webservice, and then attach metadata for the type of request and data class:
Java 5 枚举源自 Joshua Bloch 的 Effective Java(第一版)以避免 C/C++/C# 中枚举(它们只是简单的 int 常量)的陷阱以及 Java 中最终静态 int 常量的使用。
主要是 int 常量和 int 枚举不是类型安全的。您可以传入任何 int 值。在 C/C++ 中,您可以这样做:
不幸的是,Effective Java 中的类型安全枚举模式有很多样板文件,但并非全部都是显而易见的。最值得注意的是,您必须重写 private 方法
readResolve
来阻止 Java 在反序列化时创建新实例,这会破坏简单的引用检查(即使用==
运算符而不是equals()
)。因此,与 int 相比,Java 5 枚举具有以下优势:
EnumSet
和EnumMap
。与仅使用类相比,Java 5 枚举具有以下优势:
readResolve()
等);Java 5 enums originated from a typesafe enum pattern from Joshua Bloch's Effective Java (the first edition) to avoid the pitfalls of enums in C/C++/C# (which are simply thinly-veiled int constants) and the use in Java of final static int constants.
Primarily int constants and int enums aren't typesafe. You can pass in any int value. In C/C++ you can do this:
Unfortunately the typesafe enum pattern from Effective Java had a lot of boilerplate, not all of it obvious. The most notable is you had to override the private method
readResolve
to stop Java creating new instances on deserialization, which would break simple reference checking (ie using the==
operator instead ofequals()
).So Java 5 enums offer these advantages over ints:
EnumSet
andEnumMap
.Java 5 enums over these advantages over just using classes:
readResolve()
etc);除了更好的类型安全性之外,您还可以在枚举中定义自定义行为(请参阅 Effective Java< /a> 一些很好的例子)。
In addition to better type safety, you can also define custom behavior in your enums (refer to Effective Java for some good examples).
枚举已经是 Java 中的类。
如果您问为什么这更好,我会说更好的类型安全性以及除了单纯的序数值之外添加其他属性的能力。
Enums are already a class in Java.
If you're asking why this is better, I'd say that better type safety and the ability to add other attributes besides a mere ordinal value would come to mind.
您可以使用枚举来有效地实现单例^^:
You can use enums to effectively implement Singletons ^^:
将 enum 设为可以包含固定常量集的引用类型,可以实现高效的 Map 实现(如 EnumMap)和 Set 实现(如 EnumSet<) /i>(JDK 类)。
来自 EnumMap 的 javadoc :
<我>
与枚举类型键一起使用的专用 Map 实现。枚举映射中的所有键必须来自创建映射时显式或隐式指定的单个枚举类型。枚举映射在内部表示为数组。这种表示方式极其紧凑且高效。
EnumMap 将 Map 的丰富性和类型安全性与数组的速度结合起来(有效Java)。
Making enum a reference type that can contain fixed set of constants has led to efficient Map implementation like EnumMap and Set implementation like EnumSet (JDK classes).
From javadoc of EnumMap :
A specialized Map implementation for use with enum type keys. All of the keys in an enum map must come from a single enum type that is specified, explicitly or implicitly, when the map is created. Enum maps are represented internally as arrays. This representation is extremely compact and efficient.
EnumMap combines richness and type safety of Map with the speed of an array (Effective Java).
枚举本身就是一种类型 - 您不能使用不存在的枚举,也不能放入其他类似的常量。并且还可以枚举它们,使代码更加简洁。
使用静态常量可能会导致维护噩梦 - 特别是当它们分散时。
Enums are a type in itself - you cannot use an enum that does not exist, or put in some other similar looking constant. and also, you can enumerate them, so that code can be more concise.
using static constants could potentially cause maintenence nightmares - especially if they area spread out.
唯一真正的优点是它可以在
switch
语句。枚举能够完成的所有其他事情都可以使用带有private
构造函数的普通普通类来完成,该构造函数的实例又被声明为该类的public static final
字段问题(类型安全模式)。枚举的另一个优点显然是它使代码比普通类更简洁。但如果我没记错的话,在 C++(或者是 C#?)中,您可以在
switch
语句中使用String
。因此,与 C++ 相比,Java 中枚举的优势可以忽略不计。然而,Java 7 也提出了同样的建议,不确定它是否会成功。The only real advantage is that it can be used in a
switch
statement. All the other stuff an enum is capable of can just be done with plain vanilla class with aprivate
constructor whose instances in turn are declared aspublic static final
fields of the class in question (the typesafe pattern). The other advantage of enum is obviously that it makes the code less verbose than you would do with a plain vanilla class.But if I'm not mistaken, in C++ (or was it C#?) you can use a
String
in aswitch
statement. So that advantage of enums in Java is negligible as opposed to C++. However, same thing was proposed for Java 7, not sure if it will make it.使用枚举的好处:
可以创建一个对象,使其以与枚举相同的方式工作。实际上,
直到 5.0 版本,枚举才被包含在 Java 语言中。然而,
枚举使代码更具可读性,并减少程序员出错的空间。
Benefits of Using Enumerations:
An object can be created to work in the same manner as an enumeration. In fact,
enumerations were not even included in the Java language until version 5.0. However,
enumerations make code more readable and provide less room for programmer error.