JAVA Enum指定值的应用(从数据转换为Enum)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/rocklee/article/details/82346189
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

猜你喜欢

转载自blog.csdn.net/rocklee/article/details/82346189