java 来自 Enum 的常量表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16438306/
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
Constant expressions from an Enum
提问by Luis Sep
Is there any way of converting an enum into a constant expression? I want my switch operator to choose among the values of an enum, but I got a compile error "case expressions must be constant expressions", so I tried to declare it in a variable:
有没有办法将枚举转换为常量表达式?我希望我的 switch 操作符在枚举的值中进行选择,但是我得到了一个编译错误“case 表达式必须是常量表达式”,所以我试图在一个变量中声明它:
final int REG = MyEnum.REG.getIndex().intValue();
switch (service.getIndex()) {
case REG:
But I still get the same error. According to Oracle's documentation http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.28
但我仍然得到同样的错误。根据 Oracle 的文档http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.28
A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:
?Literals of primitive type and literals of type String
编译时常量表达式是表示原始类型值或不会突然完成并且仅使用以下内容组成的字符串的表达式:
? 原始类型的文字和字符串类型的文字
So it isn't working because I'm not using a literal. I think I will have to declare it as:
所以它不起作用,因为我没有使用文字。我想我必须将其声明为:
final int REG = 8;
But it'd be much better to link it to the enum. Is there any way of doing this?
但是最好将它链接到枚举。有没有办法做到这一点?
EDIT
编辑
Turns out I don't need to use any final variable. It is just as simple as:
原来我不需要使用任何最终变量。就这么简单:
switch (service) {
case REG:
It didn't occur to me till I saw Andrea's comment. Thanks for your answers.
直到我看到 Andrea 的评论我才想到。感谢您的回答。
回答by Andreas Fester
If possible, modify your getIndex()
method so that it returns an enum instead of an integer. If this is not possible, you need to mapthe index to an enum element:
如果可能,请修改您的getIndex()
方法,使其返回枚举而不是整数。如果这是不可能的,您需要将索引映射到枚举元素:
Given the following enum:
鉴于以下枚举:
public enum Index {
ONE,
TWO,
THREE
}
you can map your index to an enum element by using
您可以使用以下方法将索引映射到枚举元素
Index.values()[index]
Given your method Integer getIndex()
, you can then do something like
给定你的方法Integer getIndex()
,然后你可以做类似的事情
switch(Index.values()[getIndex()])
case ONE : ...
break;
case TWO : ...
break;
case THREE : ...
break;
}
Note that this might throw an ArrayIndexOutOfBoundsException
if you try to access an index within the enum which is larger than the number of enum elements (e.g. in the sample above, if getIndex()
returns a value > 2).
请注意,ArrayIndexOutOfBoundsException
如果您尝试访问枚举中大于枚举元素数量的索引(例如,在上面的示例中,如果getIndex()
返回值 > 2),这可能会抛出。
I would encapsulate the expression Index.values()[getIndex()]
into an enum method like valueOf(int index)
, similar to the default valueOf(String s)
. You can then also handle the valid array index check there (and for example return a special enum value if the index is out of range). Similarly, you can then also convert discrete values which have special meanings:
我会将表达式封装Index.values()[getIndex()]
到一个valueOf(int index)
类似于默认的枚举方法中valueOf(String s)
。然后,您还可以在那里处理有效的数组索引检查(例如,如果索引超出范围,则返回一个特殊的枚举值)。同样,您还可以转换具有特殊含义的离散值:
public enum Index {
ZERO,
ONE,
TWO,
THREE,
REG,
INVALID;
public static Index valueOf(int index) {
if (index == 8) {
return REG;
}
if (index >= values().length) {
return INVALID;
}
return values()[index];
}
}
This is an example only - in any case, it generally depends on the range of values you get from your getIndex()
method, and how you want to map them to the enum elements.
这只是一个示例 - 在任何情况下,它通常取决于您从您的getIndex()
方法中获得的值的范围,以及您希望如何将它们映射到枚举元素。
You can then use it like
然后你可以像这样使用它
switch(Index.valueOf(service.getIndex())) {
case ZERO : ... break;
...
case REG : ... break;
...
}
See also Cast Int to enum in Javafor some additional information (especially the hint that values()
is an expensive operation since it needs to return a copy of the array each time it is called).
另请参阅Cast Int to enum in Java以获取一些附加信息(特别是提示,这values()
是一项昂贵的操作,因为每次调用它时都需要返回数组的副本)。
回答by rzymek
If you want specific numeral values assigned to enum constacts go like this
如果你想分配给枚举常量的特定数字值像这样
enum MyReg {
REG(8), OTHER(13);
public final int value;
MyReg(int value) {
this.value=value;
}
}
You then use it like this:
然后像这样使用它:
class Test {
public static void main(String[] args) {
MyReg reg = MyReg.REG;
switch (reg) {
case OTHER:
System.out.println(reg.value);
break;
case REG:
System.out.println(reg.value);
break;
}
}
}