一个 Java 文件中的多个枚举类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10017729/
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
Multiple Enum Classes in one Java File
提问by popcoder
I have 3 String arrays with constants. eg:
我有 3 个带有常量的字符串数组。例如:
String[] digit = {"one", "two", "three"};
String[] teen= {"ten", "twenty", "thirty"};
String[] anchors = {"hundred", "thousand", "million"};
I'm thinking of transferring these to enums separately, so I will have 3 enum classes: digit
, teen
and anchors
with getValue
methods implemented. But I don't want to have them in separate files as I have only small data and same type of data. What is the best way to have all these with access methods in same meaningful java file?
我想单独转让这些来枚举的,所以我将有3枚举类:digit
,teen
并anchors
用getValue
方法来实现。但我不想将它们放在单独的文件中,因为我只有小数据和相同类型的数据。在同一个有意义的java文件中使用访问方法的最佳方法是什么?
回答by Eugene Retunsky
They can be three inner classes like this:
它们可以是三个内部类,如下所示:
public class Types {
public enum Digits {...}
public enum Teens {...}
....
}
Then refer them Types.Digits.ONE
, Types.Teen.TWENTY
etc.
然后参考他们Types.Digits.ONE
,Types.Teen.TWENTY
等等。
You can also use static imports like this:
您还可以像这样使用静态导入:
import Types.Digits;
import Types.Teen;
..
..
in order to have shorter references: Digits.ONE
, Teen.TWENTY
etc.
为了有更短的引用:Digits.ONE
,Teen.TWENTY
等等。