java 枚举 VS 类 VS 接口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6144227/
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
Enums VS Classes VS Interfaces
提问by haju
I have been reading a lot of posts on this site regarding the usage of constants.
我在这个网站上阅读了很多关于常量使用的帖子。
Question: When should I use Enums for constants, vs using classes or interfaces.
问题:我什么时候应该使用枚举作为常量,而不是使用类或接口。
I see 2 key situations I am looking to address.
我看到了我希望解决的 2 个关键情况。
1. Global Constants used in a applications by multiple projects.
1. 多个项目在一个应用程序中使用的全局常量。
Example:
例子:
- Common logging strings
- Container references like a database mapping reference used in WebSphere EAR's
- 常用日志记录字符串
- 容器引用,如 WebSphere EAR 中使用的数据库映射引用
2. Object Specific Constants
2. 对象特定常量
Example:
例子:
- Employee pay rates for an Employee Object
- 员工对象的员工工资率
From everything I have read this is what I think I have a grasp on and what I am looking for an opinion on.
从我阅读的所有内容中,这是我认为我已经掌握的内容以及我正在寻找的意见。
For situation 1:
Design Approach: Use a final class and a static import.
Seen here: What is the use of interface constants?
对于情况 1:设计方法:使用最终类和静态导入。
在这里看到:接口常量的用途是什么?
For Situation 2: Design Approach: Apply the use of Enums to represent those constants as a object.
对于情况 2:设计方法:应用枚举的使用将这些常量表示为对象。
Additional points to remember:
需要记住的其他要点:
- If the constant string belongs to the class and you only need the string value keep in the class that uses it
- Don't use an Interface for situation 1. As mentioned in the link above as Constant Interface Anti-pattern. .
- 如果常量字符串属于类并且您只需要将字符串值保留在使用它的类中
- 不要在情况 1 中使用接口。如上面的链接中提到的常量接口反模式。.
Thanks in advance for thoughts and opinions.
在此先感谢您的想法和意见。
采纳答案by Woot4Moo
Global constants as you put it should actually be in a properties file as it allows each application to configure them individually without a code modification. For object specific constants my general rule of thumb on Enum
versus static final
I typically lean towards how many elements there are to have and how related those elements are. If there is a big relation between them such as Suits
in a deck of Cards
then I would go for the enum. If it is default age for a user, then this becomes a final as there is no purpose to making it an enum as it would not need to be referenced in many areas. These are just some thoughts on each of the ways I have approached it.
您放置的全局常量实际上应该在属性文件中,因为它允许每个应用程序单独配置它们而无需修改代码。对于特定于对象的常量,我的一般经验法则Enum
与static final
我通常倾向于有多少元素以及这些元素的相关性。如果它们之间有很大的关系,例如Suits
在甲板上,Cards
那么我会去枚举。如果它是用户的默认年龄,那么这将成为最终的,因为没有目的将其设为枚举,因为它不需要在许多领域被引用。这些只是对我处理它的每种方式的一些想法。
回答by zengr
Global constants used by different projects:
Enum
Better to useEnum
overpublic static final
members in a class. More clean and easy to understand I guess.Object specific constants:
public static final members in Class
. Because, they are needed only within the scope of the object, then no need to created a new Enum for that.
通过不同的项目中使用全局常量:
Enum
最好使用Enum
在public static final
成员的类。我想更干净,更容易理解。对象特定常量:
public static final members in Class
. 因为,它们只在对象的范围内需要,那么不需要为此创建一个新的 Enum。
Nice read
好读
Update (fixed broken link):
更新(修复断开的链接):
回答by nsfyn55
It sounds like almost everything you've listed for both numbers 1 and 2 belong in configuration files or database tables.
听起来您为数字 1 和 2 列出的几乎所有内容都属于配置文件或数据库表。
Do you want to re-compile code when your employee's get a raise or a page name changes?
当您的员工获得加薪或页面名称更改时,您想重新编译代码吗?
Unless there is a compelling reason everything else that is constant should be modeled as an enum. This way you realize the benefits of fast object equality comparisons and you avoid problems associated String constants.
除非有令人信服的理由,否则所有其他常量都应该建模为枚举。通过这种方式,您可以实现快速对象相等比较的好处,并避免与 String 常量相关的问题。
The scope of those enums however is application specific. If the enumeration is only used by a class that it should be a private enum. If it is shared by multiple classes then it should be in its own class definition file.
然而,这些枚举的范围是特定于应用程序的。如果枚举仅由类使用,则它应该是私有枚举。如果它被多个类共享,那么它应该在它自己的类定义文件中。