50,530
社区成员
发帖
与我相关
我的任务
分享
import static enumerated.Signal;
// Define an enum type:
//enum Signal { GREEN, YELLOW, RED, }
//public class TrafficLight {
public class ChapterEnumExe1 {
Signal color = Signal.RED;
public void change() {
switch(color) {
// Note that you don't have to say Signal.RED
// in the case statement:
case RED: color = Signal.GREEN;
break;
case GREEN: color = Signal.YELLOW;
break;
case YELLOW: color = Signal.RED;
break;
}
}
public String toString() {
return "The traffic light is " + color;
}
public static void main(String[] args) {
ChapterEnumExe1 t = new ChapterEnumExe1();
for(int i = 0; i < 7; i++) {
System.out.println(t);
t.change();
}
}
} /* Output:
The traffic light is RED
The traffic light is GREEN
The traffic light is YELLOW
The traffic light is RED
The traffic light is GREEN
The traffic light is YELLOW
The traffic light is RED
*///:~
//ernumerated/Signal.java
package enumerated;
public enum Signal { GREEN, YELLOW, RED, }
import static net.mindview.util.Print.*;