A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

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);
        }

}

2 个回复

正序浏览
孙利川 发表于 2012-4-4 17:05
abstract class Lamp {
        private int time;
        private Lamp(){}


好吧  我又犯2了  唉 郁闷了  谢谢了   Eclipse  居然不报错  晕死
回复 使用道具 举报
abstract class Lamp {
        private int time;
        private Lamp(){}
        private Lamp(int tiem){      //这里出错了,time写错了
                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?               
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马