编码约定 - 命名枚举 [英] 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屋!