当前位置: 代码迷 >> J2SE >> 关于枚举的有关问题···
  详细解决方案

关于枚举的有关问题···

热度:5707   发布时间:2013-02-25 00:00:00.0
关于枚举的问题···
//功能:输出交通灯的信号
enum TracfficLight{ //内部枚举
GREEN,RED,YELLOW; //信号灯三种情况
private TracfficLight color;
TracfficLight(TracfficLight color){ //枚举的构造方法
this.color=color;}
//输出上面的值并输出信号值的方法
public void putAll(){
for(TracfficLight t:TracfficLight.values()){
System.out.print(t.name());
System.out.println(" the ordinal is "+t.ordinal());
}
}
//不同的输入显示不同的灯的信息的方法
public void prinDiffer(TracfficLight color){

switch(color){
case GREEN: System.out.println("绿灯亮了。");break;
case RED: System.out.println("红灯亮了。");break;
case YELLOW: System.out.println("黄灯亮了。");break;
}
}
}

第三行出了错,不知道怎么回事啊???

------解决方案--------------------------------------------------------
Java code
    // 功能:输出交通灯的信号    enum TracfficLight { // 内部枚举        GREEN(12,23,4), RED(12,34,34), YELLOW(12,12,12); // 信号灯三种情况        int red;        int green;        int yellow;        TracfficLight(int red,int green,int yellow) { // 枚举的构造方法            this.red = red;            this.green = green;            this.yellow = yellow;        }        // 输出上面的值并输出信号值的方法        public void putAll() {            for (TracfficLight t : TracfficLight.values()) {                System.out.print(t.name());                System.out.println(" the ordinal is " + t.ordinal());            }        }        // 不同的输入显示不同的灯的信息的方法        public void prinDiffer(TracfficLight color) {            switch (color) {            case GREEN:                System.out.println("绿灯亮了。");                break;            case RED:                System.out.println("红灯亮了。");                break;            case YELLOW:                System.out.println("黄灯亮了。");                break;            }        }    }
  相关解决方案