JAVA Enum指定值的應用(從資料轉換為Enum)
阿新 • • 發佈:2018-11-17
enum ACTION{ ac_Entry(10),ac_View(2),ac_Approve(5); private int _val; private static final Map<Integer,ACTION> keyMap=new HashMap<Integer,ACTION>(); static{ for (ACTION item:ACTION.values()){ keyMap.put(item._val,item); } } public int getVal(){ return _val; } ACTION(int val){ _val=val; } public static ACTION fromVal(int pvnVal){ return keyMap.get(pvnVal); } } @Test public void testEnum(){ ACTION e=ACTION.ac_View; System.out.println(e+","+e.ordinal()+","+e.getVal()); e=ACTION.fromVal(5); System.out.println(e+","+e.ordinal()+","+e.getVal()); }
原理,就是用一個map來進行value->enum的轉換.
ac_View,1,2
ac_Approve,2,5