java学习笔记----枚举测试题

定义义一个交通灯枚举类,包含红灯、绿灯、黄灯,需要有获得下一个灯的方法,并实现红灯出现5秒之后变成绿灯,绿灯3秒之后变成黄灯,黄灯2秒之后变成红灯,如此循环
 public class Test5 {

      /**
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
Deng[] deng = Deng. values();
for( int i = 0;i < 3;i++){
Deng d = deng[i];
if(d == Deng. red){
System. out.println(d);
Thread. sleep(5000);
System. out.println(d.next());
} else if(d == Deng. green){
Thread. sleep(3000);
System. out.println(d.next());
} else if(d == Deng. yellow){
Thread. sleep(2000);
// System.out.println(d.next());
i = -1;
}
}
}
}
enum Deng{
red("红灯"){
public Deng next(){
return green;
}
},green("绿灯"){
public Deng next(){
return yellow;
}
},yellow("黄灯"){
public Deng next(){
return red;
}
};
public abstract Deng next();
private String name;
private Deng(String name){
this. name = name;
}
@Override
public String toString() {
return name;
}
}
上一篇:Python进阶----pymysql模块的使用,单表查询


下一篇:JSFunction-Javascript常用函数库