1. 程式人生 > >enum用ordinal獲取物件

enum用ordinal獲取物件

package test;

import java.io.IOException;

public class t1 {
	public enum action{
		answer,learn,query;
		public static action valueOf(int ordinal) {
		  if (ordinal < 0 || ordinal >= values().length) {
		      throw new IndexOutOfBoundsException("Invalid ordinal");
		  }
		  return values()[ordinal];
		 }
	}
	
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		action a = action.answer;
		System.out.println(a.valueOf(2));
	}
	

}