在交通灯的RED,GREEN,YELLOW中,这些对象可以直接有子类,实现方法,看上去很别扭,怎么理解?
public enum TrafficLamp{
RED(30){//TrafficLamp子类及其实现
public TrafficLamp nextLamp(){
return GREEN;
}
},
GREEN(45){
public TrafficLamp nextLamp(){
return YELLOW;
}
},
YELLOW(5){
public TrafficLamp nextLamp(){
return RED;
}
};
public abstract TrafficLamp nextLamp();
private int time;
private TrafficLamp(int time){
this.time = time;}
public int getTime() {
return time;
}
} |