Java 5+ 中的静态字符串常量 VS 枚举

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1858829/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:21:45  来源:igfitidea点击:

Static String constants VS enum in Java 5+

javaenumsconstants

提问by EugeneP

I've read that question & answers: What is the best way to implement constants in Java?

我已经阅读了这个问题和答案: 在 Java 中实现常量的最佳方法是什么?

And came up with a decision that enum is better way to implement a set of constants. Also, I've read an example on Sun web site how to add the behaviour to enum (see the link in the previously mentioned post). So there's no problem in adding the constructor with a String key to the enum to hold a bunch of String values.

并提出了一个决定,即枚举是实现一组常量的更好方法。另外,我在 Sun 网站上阅读了如何将行为添加到 enum 的示例(请参阅前面提到的帖子中的链接)。因此,将带有 String 键的构造函数添加到枚举以保存一堆 String 值是没有问题的。

The single problem here is that we need to add ".nameOfProperty" to get access to the String value. So everywhere in the code we need to address to the constant value not only by it's name (EnumName.MY_CONSTANT), but like that (Enum.MY_CONSTANT.propertyName).

这里的唯一问题是我们需要添加“.nameOfProperty”来访问字符串值。所以在代码中的任何地方,我们不仅需要通过它的名称(EnumName.MY_CONSTANT)来寻址常量值,而且像这样(Enum.MY_CONSTANT.propertyName)。

Am I right here? What do you think of it?

我就在这里吗?你怎么看呢?

采纳答案by KLE

Yes, the naming may seem a bit longer. But not as much as one could imagine...

是的,命名可能看起来有点长。但并没有想象中的那么多……

  1. Because the enum class already give some context ("What is the set of constants that this belong to?"), the instance name is usually shorter that the constant name(strong typing already discriminated from similar named instances in other enums).

  2. Also, you can use static importsto further reduce the length. You shouldn't use it everywhere, to avoid confusions, but I feel that a code that is strongly linked to the enum can be fine with it.

  3. In switcheson the enum, you don't use the class name. (Switches are not even possible on Strings pre Java 7.)

  4. In the enum class itself, you use the short names.

  5. Because enums have methods, many low-level codes that would make heavy use of the constants could migrate from a business code to the enum class itself (either dynamic or static method). As we saw, migrating code to the enumreduces the long names uses even further.

  6. Constants are often treated in groups, such as an ifthat test for equality with one of six constants, or four others etc. Enums are equipped with EnumSetswith a containsmethod (or similarly a dynamic method that returns the appropriate group), that allow you to treat a group as a group(as a secondary advantage, note that these two implementations of the grouping are extraordinarily fast - O(1) - and low on memory!).

  1. 因为枚举类已经给出了一些上下文(“它属于什么常量集?”),实例名称通常比常量名称更短(强类型已经与其他枚举中的类似命名实例区分开来)。

  2. 此外,您可以使用静态导入来进一步减少长度。你不应该在任何地方使用它,以避免混淆,但我觉得与枚举密切相关的代码可以很好地使用它。

  3. 在枚举的开关中,您不使用类名。(在 Java 7 之前的 Strings 上甚至不可能切换。)

  4. 在枚举类本身中,您使用短名称。

  5. 因为枚举有方法,许多会大量使用常量的低级代码可以从业务代码迁移到枚举类本身(动态或静态方法)。正如我们所看到的,将代码迁移到枚举进一步减少了长名称的使用。

  6. 常量通常按组处理,例如if测试与六个常量之一或其他四个等的相等性。枚举配备EnumSets了一个contains方法(或类似的返回适当组的动态方法),允许您处理一个组作为一个组(作为次要优势,请注意分组的这两种实现非常快 - O(1) - 并且内存不足!)。

With all these points, I found out that the actual codes are much much shorter!

有了所有这些点,我发现实际代码要短得多

回答by Nick Fortescue

With regard to the question about constants - enums should represent constants that are all the same type. If you are doing arbitrary constants this is the wrong way to go, for reasons all described in that other question.

关于常量的问题 - 枚举应该代表所有相同类型的常量。如果您正在执行任意常量,这是错误的方法,原因在其他问题中都有描述。

If all you want are String constants, with regard to verbose code you are right. However, you could override the toString() method return the name of the property. If all you want to do is concatenate the String to other Strings then this will save you some extra verbosity in your code.

如果您想要的只是字符串常量,那么对于冗长的代码,您是对的。但是,您可以覆盖 toString() 方法返回属性的名称。如果您只想将字符串连接到其他字符串,那么这将为您节省一些额外的代码冗长。

However, have you considered using Properties files or some other means of internationalisation? Often when defining dets of Strings it is for user interface messages, and extracting these to a separate file might save you a lot of future work, and makes translation much easier.

但是,您是否考虑过使用属性文件或其他一些国际化方法?通常在定义字符串的 det 时,它用于用户界面消息,将它们提取到单独的文件中可能会为您节省大量的未来工作,并使翻译更容易。