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

定义义一个交通灯枚举类,包含红灯、绿灯、黄灯,需要有获得下一个灯的方法,并实现红灯出现5秒之后变成绿灯,绿灯3秒之后变成黄灯,黄灯2秒之后变成红灯,如此循环
 1 public class Test5 {
 2 
 3      /**
 4       * @throws InterruptedException
 5       */
 6      public static void main(String[] args) throws InterruptedException {
 7           Deng[] deng = Deng. values();
 8            for( int i = 0;i < 3;i++){
 9               Deng d = deng[i];
10                if(d == Deng. red){
11                    System. out.println(d);
12                    Thread. sleep(5000);
13                    System. out.println(d.next());
14               } else if(d == Deng. green){
15                    Thread. sleep(3000);
16                    System. out.println(d.next());
17               } else if(d == Deng. yellow){
18                    Thread. sleep(2000);
19 //                 System.out.println(d.next());
20                    i = -1;
21               }
22           }
23      }
24 }
25 enum Deng{
26      red("红灯"){
27            public Deng next(){
28                return green;
29           }
30      },green("绿灯"){
31            public Deng next(){
32                return yellow;
33           }
34      },yellow("黄灯"){
35            public Deng next(){
36                return red;
37           }
38      };
39      public abstract Deng next();
40      private String name;
41      private Deng(String name){
42            this. name = name;
43      }
44      @Override
45      public String toString() {
46            return name;
47      }
48 }

 

posted @ 2015-10-02 17:14 卑微幻想家 阅读(...) 评论(...) 编辑 收藏