本帖最后由 谢洋 于 2013-2-25 10:34 编辑
package cn.itcast.test;
/**
* 复杂的枚举类
* 为什么加载时没有执行成员子类的打印语
*/
public class EnumTest {
public static void main(String[] args) {
TrafficLamp yellow = TrafficLamp.YELLOW;
System.out.println(yellow.toString());
}
public enum TrafficLamp{
//通过子类完成子类对象实例
//并且调用父类的有参构造方法
//子类必须完成父类的构造方法
RED(30){ //private static final RED()//这不是静态的?类加加载时为什么没有执行打印语句?
public TrafficLamp nextLamp(){
System.out.println("red");
return GREEN;
}
},
GREEN(45){
public TrafficLamp nextLamp(){
System.out.println("green");
return YELLOW;
}
},
YELLOW(5){
public TrafficLamp nextLamp(){
System.out.println("yellow");
return RED;
}
};
public abstract TrafficLamp nextLamp();
private int time;
private TrafficLamp(int time){//隐式的static
this.time = time;
}
}
}
|