Java 5+中的静态字符串常量VS枚举 [英] Static String constants VS enum in Java 5+

查看:138
本文介绍了Java 5+中的静态字符串常量VS枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过这个问题&答案:
什么是最好的方法在Java中实现常量?



并提出了一个决定,枚举是更好的方法来实现一组常量。
另外,我已经在Sun网站上看到一个例子,将行为添加到枚举中(参见前面提到的帖子中的链接)。
因此,使用String键将构造函数添加到枚举中以保存一串String值没有问题。



这里的单个问题是我们需要添加.nameOfProperty来访问String值。
所以在代码中无处不在,我们需要处理恒定值,不仅仅是它的名字(EnumName.MY_CONSTANT),而是像这样(Enum.MY_CONSTANT.propertyName)。


$ b $我是在这里吗?你觉得怎么样?

解决方案

是的,命名可能会更长一些。但是没有人能想像的那样...


  1. 因为枚举类已经给了一些上下文(是这个属性的常量集?),实例名称通常比常数名称更短(强类型已经与其他枚举中的类似命名实例区分开) p>


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


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


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


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


  6. 常量常常被分组处理作为一个,如果测试与六个常量之一相等的四个等等。枚举配有 EnumSets 与一个包含方法(或类似的返回相应组的动态方法),允许您将组视为组(作为辅助优点,请注意,这两个分组的实现非常快 - O(1) - 低内存!)。




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


I've read that question & answers: What is the best way to implement constants in 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.

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).

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.)

  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!).

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

这篇关于Java 5+中的静态字符串常量VS枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆