Week week = Week.SUNDAY;int i = week.ordinal();
但是请注意,如果您更改声明中的枚举常量的顺序,则此值将更改。解决此问题的一种方法是将int值自定义为所有枚举常量,如下所示:
public enum Week { SUNDAY(0), MonDAY(1) private static final Map<Integer,Week> lookup= new HashMap<Integer,Week>(); static { for(Week w : EnumSet.allOf(Week.class)) lookup.put(w.getCode(), w); } private int pre; private Week(int pre) { this.pre = pre; } public int getCode() { return pre; } public static Week get(int pre) {return lookup.get(pre); }}


