老师讲的交通灯这个例子,3个枚举元素,因为枚举类中有抽象方法,所以3个元素要实现的话必须覆盖抽象方法,如果要有构造函数,想去调用需在元素后面调用的对应的参数。是这样理解吗?
这个类 能有哪些用处?
public enum TrafficLamp{
RED(30) {
@Override
public TrafficLamp nextLamp() {
// TODO Auto-generated method stub
return GREEN;
}
},
GREEN(40) {
@Override
public TrafficLamp nextLamp() {
// TODO Auto-generated method stub
return YELLOW;
}
},
YELLOW(5) {
@Override
public TrafficLamp nextLamp() {
// TODO Auto-generated method stub
return RED;
}
};
public abstract TrafficLamp nextLamp();
private int time;
private TrafficLamp(int time){
this.time = time;
}
}
}
|