为什么使用常量而不是枚举?
我在很多很多 Java 库中看到了很多可以轻松使用枚举的常量。即使在 Swing 中,也有很多代码使用常量而不是枚举。为什么?
使用枚举有什么缺点?
I've seen in lots and lots of Java libraries the use of lots of constants where enums could have easily been used. Even in Swing, there is a lot of code that uses constants instead of enums. Why?
What are the disadvantages to using enums?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因为枚举是在 Java 5 中引入的,而这些库很早之前就已经编写好了。重构它们将破坏大量现有应用程序。
Because enums were introduced in Java 5, and those libraries have been written long before. Refactoring them would break a bazillion of existing applications.
我发现我仍然告诉人们使用枚举作为单例,尽管它已经存在了 7 年了。 :P
什么时候会使用常量而不是枚举?当您有许多关联但不直接相关的常量时。
I find I still telling people to use enum for a Singleton even though it has been around for 7 years now. :P
When would use use constants instead of enum? When you have many associated but not directly related constants.
java中的Enum是在Java 5中引入的,以前没有这些,Enum相当于java中的类。
Enums in java are introduced in Java 5, these were not there before and Enum is equivalent to a class in java.
在 Java 1.5 之前,Java 中没有枚举,实现枚举模式最直接的方法是使用 const int。您可以在 枚举的 Java 教程部分。
Prior to Java 1.5, there was no
enum
in Java, and the most straight-forward way to implement the enum pattern was to use const ints. You can read more in the Java Tutorial section for enums.