编码约定 - 命名枚举 [英] Coding Conventions - Naming Enums
问题描述
Java 中是否有命名枚举的约定?
Is there a convention for naming enumerations in Java?
我的偏好是枚举是一种类型.因此,例如,您有一个枚举
My preference is that an enum is a type. So, for instance, you have an enum
Fruit{Apple,Orange,Banana,Pear, ... }
NetworkConnectionType{LAN,Data_3g,Data_4g, ... }
我反对命名:
FruitEnum
NetworkConnectionTypeEnum
我知道很容易挑选出哪些文件是枚举,但你也会有:
I understand it is easy to pick off which files are enums, but then you would also have:
NetworkConnectionClass
FruitClass
另外,是否有一个很好的文档来描述常量,在哪里声明它们等?
Also, is there a good document describing the same for constants, where to declare them, etc.?
推荐答案
枚举是类,应该遵循类的约定.枚举的实例是常量,应该遵循常量的约定.所以
Enums are classes and should follow the conventions for classes. Instances of an enum are constants and should follow the conventions for constants. So
enum Fruit {APPLE, ORANGE, BANANA, PEAR};
没有理由编写 FruitEnum
和 FruitClass
一样.您只是在浪费四个(或五个)没有添加任何信息的字符.
There is no reason for writing FruitEnum
any more than FruitClass
. You are just wasting four (or five) characters that add no information.
Java 本身推荐这种方法,并且在他们的示例中使用一>.
Java itself recommends this approach and it is used in their examples.
这篇关于编码约定 - 命名枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!