一個列舉的例子
package cn.itcast;
import java.util.Date;
public class EnumTest {
public static void main(String[] args) {
WeekDay weekday1 = WeekDay.MON;
//System.out.println(weekday1.nextDay());
//System.out.println(weekday);
WeekDay weekDay2 = WeekDay.FRI;
System.out.println(weekDay2);
System.out.println(weekDay2.name());
System.out.println(weekDay2.ordinal());
System.out.println(WeekDay.valueOf("SUN").toString());
System.out.println(WeekDay.values().length);
new Date(300){};
}
public enum WeekDay{
//構造方法必須寫在列舉元素之後
//如果在列舉元素之後沒有寫括號,預設呼叫的是無參構造
//列舉的構造方法必須是private型別的
//如果在列舉元素之後帶括號,這時呼叫的是有參構造。
SUN(),MON(),TUE(),WED,THT,FRI,SAT;
private WeekDay(){
System.out.println("first");
}
private WeekDay(int day){
System.out.println("second");
}
}
public enum TrafiicLamp{
RED(30) {
public TrafiicLamp nextLamp() {
return GREEN;
}
},
GREEN(45) {
public TrafiicLamp nextLamp() {
return YELLOW;
}
},
YELLOW(5) {
public TrafiicLamp nextLamp() {
return RED;
}
};
public abstract TrafiicLamp nextLamp();
private int time;
private TrafiicLamp(int time){
this.time = time;
}
}
}