枚举本身是一种类型,枚举类型里面的成员是实际对象
public class EnumTest {
public static void main(String argsp[]) {
System.out.println(TrafficLamp.RED.time);
}
public enum TrafficLamp {
RED(30) {
public TrafficLamp nextLamp() {
return GREEN;
}
},
YELLOW(5) {
public TrafficLamp nextLamp() {
return RED;
}
},
GREEN(30) {
public TrafficLamp nextLamp() {
return YELLOW;
}
};
public abstract TrafficLamp nextLamp();
private int time;
private TrafficLamp(int time) {
this.time = time;
}
}
}
以上,典型枚举
另外:枚举只有一个成员时,就可以作为一种单例的实现方式