枚举类:
public enum Light {
RED{
@Override
public Light nextLight() {
// TODO Auto-generated method stub
System.out.println("红灯转绿灯-----等待 5秒");
getStop(1000*5);
return GREEN;
}
},
GREEN{
@Override
public Light nextLight() {
System.out.println("绿灯转黄灯-----等待 3秒");
getStop(1000*3);
return YELLOW;
}
},
YELLOW{
@Override
public Light nextLight() {
System.out.println("黄灯转红灯-----等待 2秒");
getStop(1000*2);
return RED;
}
};
public abstract Light nextLight();
public void getStop(int time) {
try {
Thread.sleep(time);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
===================================================================================
测试方法:
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Light light1 = Light.RED;
while (true) {
light1.nextLight().nextLight().nextLight();
}
}
}
|
|