1. 程式人生 > >實現帶有構造方法的列舉,索引從0開始

實現帶有構造方法的列舉,索引從0開始

/**
 * 
 * @author juvie
 *  所有的列舉元素都是靜態成員變數,在初始化時都會呼叫建構函式。
 */
public class EnumTest {
	
	public enum WeekDay{
		SUN(1),MON(),TUE,WED,THI,FRI,SAT;
		private WeekDay(){
			System.out.println("first");
		}
		private WeekDay(int num){
			System.out.println("second");
		}
	}
	
	public static void main(String[] args) {
		WeekDay wk=WeekDay.SUN;
		System.out.println(wk);
		System.out.println(wk.name());
		System.out.println(wk.ordinal());
	}

}
second
first
first
first
first
first
first
SUN
SUN
0