码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-10-02 18:37:41      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

定义义一个交通灯枚举类,包含红灯、绿灯、黄灯,需要有获得下一个灯的方法,并实现红灯出现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 }

 

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

标签:

原文地址:http://www.cnblogs.com/junzhao/p/4852424.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!