【发布时间】:2016-07-22 06:05:19
【问题描述】:
大家好,我正在尝试访问内部类中存在的枚举值,如下所示,但我得到的不是值而是键。我的应用程序的需要是我必须通过反射来访问这个值。
public class Test{
static class TwelveByTwentyFour {
public static enum BET_TYPE_NAME {
Direct12(12),AllOdd(12),AllEven(12), First12(12), Last12(12);
private int value;
BET_TYPE_NAME(int value){
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
}
public static String getBetTypeLength(String gameName,String betType) throws ClassNotFoundException, IllegalArgumentException, SecurityException, IllegalAccessException, NoSuchFieldException{
return Class.forName(Test.class.getCanonicalName()+"$"+gameName+"$"+"BET_TYPE_NAME").getDeclaredField(betType).get(null).toString();
}
public static void main(String[] args) throws IllegalArgumentException, SecurityException, ClassNotFoundException, IllegalAccessException, NoSuchFieldException {
System.out.println(getBetTypeLength("TwelveByTwentyFour", "AllEven"));
}
}
在执行此操作时,我得到"AllEven" 作为输出而不是"12"。任何人都可以告诉我如何获得价值来帮助我。
【问题讨论】:
标签: java class reflection enums inner-classes