求教枚举的使用问题 不知道为什么以下用法是错误的 提示 case expressions must be constant expressions
Java code
public class TestEnum {
public static void main(String[] args) {
int val = 2;
switch (val) {
case INT.ONE.getVal():
break;
default:
break;
}
}
}
enum INT {
ONE(1), TWO(2), THREE(3);
private int val;
private INT(int val) {
this.val = val;
}
/**
* @return the val
*/
public int getVal() {
return val;
}
}
------解决方案-------------------- 你应该这样用[code=Java][/code]public class Test11 {
public static void main(String[] args) { INT v=INT.ONE; int val; switch (v) { case ONE: val=1; break;
default: break; } } }
enum INT { ONE(1), TWO(2), THREE(3);
private int val;
private INT(int val) { this.val = val; }
/** * @return the val */ public int getVal() { return val; }