我有枚举,例如:
public enum Type {
Type1(10),
Type2(25),
Type3(110);
private final int value;
Type(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
我想在 switch 中使用它枚举:
switch (indexSector) {
case Type.Type2.getValue():
//...
break;
}
但是 IDE 说“需要常量表达式”。如何在开关中使用 Enum 这种类型?
最佳答案
Type indexSector = ...;
int value = indexSector.getValue();
switch (indexSector) {
case Type2:
// you can use the int from the value variable
//...
break;
}
关于java - 带有构造函数的枚举并在 switch JAVA 中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26191399/