java枚舉比如一個構造方法私有化的類,其枚舉變量就是類加載時,實例化好的該類型的靜態成員變量。java
/*this
*交通燈spa
*/對象
public enum TrafficLight{tls
RED(40){變量
public TrafficLight next(){方法
return TrafficLight.GREEN;im
}next
},GREEN(50){co
public TrafficLight next(){
return TrafficLight.YELLOW;
}
},YELLOW(10){
public TrafficLight next(){
return TrafficLight.RED;
}
};
private int time;
private TrafficLight(int time){
this.time=time;
}
public abstract TrafficLight next();
}
枚舉實用方法:
TrafficLight tl = TrafficLight.valueOf("GREEN");//根據名稱獲取枚舉對象 TrafficLight[] tls = TrafficLight.values();//獲取全部的枚舉成員對象 System.out.println(tl.name());//GREEN System.out.println(tl==TrafficLight.GREEN);//true