简体   繁体   English

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

[英]Static String constants VS enum in Java 5+

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). 另外,我在Sun网站上阅读了一个如何将行为添加到枚举的示例(请参阅前面提到的帖子中的链接)。 So there's no problem in adding the constructor with a String key to the enum to hold a bunch of String values. 因此,使用String键将构造函数添加到枚举中以保存一堆String值没有问题。

The single problem here is that we need to add ".nameOfProperty" to get access to the String value. 这里的唯一问题是我们需要添加“.nameOfProperty”来访问String值。 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). 所以代码中的每个地方我们都需要通过它的名称(EnumName.MY_CONSTANT)来解决常量值,但是就像那样(Enum.MY_CONSTANT.propertyName)。

Am I right here? 我在这儿吗? What do you think of it? 你怎么看呢?

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 imports to 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 switches on the enum, you don't use the class name. 在枚举的开关中 ,您不使用类名。 (Switches are not even possible on Strings pre Java 7.) (在Java 7之前的Strings上甚至不能使用开关。)

  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 enum reduces the long names uses even further. 正如我们所看到的,将代码迁移到枚举会进一步减少长名称的使用。

  6. Constants are often treated in groups, such as an if that test for equality with one of six constants, or four others etc. Enums are equipped with EnumSets with a contains method (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!). 常量通常以组的形式处理,例如, if使用六个常量之一进行相等性测试,或者另外四个等等。枚举配备带有contains方法的EnumSets (或类似地返回适当组的动态方法),允许您将组视为一个组 (作为次要优势,请注意这两个分组实现非常快 - O(1) - 内存不足!)。

With all these points, I found out that the actual codes are much much shorter ! 有了所有这些要点, 我发现实际的代码要短得多

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. 但是,您可以覆盖toString()方法返回属性的名称。 If all you want to do is concatenate the String to other Strings then this will save you some extra verbosity in your code. 如果您只想将String连接到其他字符串,那么这将为您的代码节省一些额外的冗长。

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. 通常在定义Strings的dets时,它用于用户界面消息,将这些消息提取到单独的文件可能会为您节省大量的未来工作,并使翻译更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM