abstract class Lamp {
private int time;
private Lamp(){}
private Lamp(int tiem){
this.time = time;
System.out.println("time="+time); //这句话打印出来为什么time没有赋值? }
public abstract Lamp nextLamp();
public final static Lamp redLamp = new Lamp(45){ //Lamp的子类匿名内部类 new Lamp(45) 调用了父类带参数的
// 构造方法 为什么 time=0 而45也没有传值给父类构造函数中的time? @Override
public Lamp nextLamp() {
// TODO Auto-generated method stub
return greenLamp;
}
};
public final static Lamp greenLamp = new Lamp(45){
@Override
public Lamp nextLamp() {
// TODO Auto-generated method stub
return yelloLamp;
}
};
public final static Lamp yelloLamp = new Lamp(5){
@Override
public Lamp nextLamp() {
// TODO Auto-generated method stub
return redLamp;
}
};
@Override
public String toString(){
if(this==redLamp)
return "红灯,时间是:"+this.time;
else if(this==greenLamp)
return "绿灯,时间是:"+this.time;
else if(this==yelloLamp)
return "黄灯,时间是:"+this.time;
return null;
}
}
public class EnumTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Lamp redLamp = Lamp.redLamp.nextLamp();
Lamp greenLamp = Lamp.greenLamp.nextLamp();
Lamp yelloLamp = Lamp.yelloLamp.nextLamp();
System.out.println("红灯的下一个灯是:"+redLamp);
System.out.println("绿灯的下一个灯市:"+greenLamp);
System.out.println("黄灯的下一个灯市:"+yelloLamp);
System.out.println("redLamp是:"+Lamp.redLamp);
System.out.println("greenLamp是:"+Lamp.greenLamp);
System.out.println("yelloLamp是:"+Lamp.yelloLamp);
}
} |
|