java 枚举2

 1 package com.wsy.test;
 2 
 3 public enum Color {
 4     RED("红色",1),GREEN("绿色",2),BLUE("蓝色",3);
 5     private String name;
 6     private int index;
 7     private Color(String name , int index) {
 8         this.name = name;
 9         this.index = index;
10     }
11     @Override
12     public String toString() 
13     {
14         return "颜色为:"+this.name+"    序号为:"+this.index;
15     }
16 }
1 package com.wsy.test;
2 
3 public enum SexEnum {
4     MALE,FAMALE;
5     
6 }
 1 package com.wsy.test;
 2 
 3 public class Test {
 4 
 5     public static void main(String[] args) {
 6         Color c1 = Color.RED;
 7         switch(c1)
 8         {
 9             case RED:
10                 c1 = Color.BLUE;
11                 break;
12             case BLUE:
13                 c1 = Color.GREEN;
14                 break;
15             case GREEN:
16                 c1 = Color.RED;
17                 break;
18             default:
19                 c1 = null;
20                 break;
21         }
22         System.out.println(c1);
23     }
24 
25 }
 1 package com.wsy.test;
 2 
 3 public class Test2 {
 4 
 5     public static void main(String[] args) {
 6 //        for(int i =0; i<Color.values().length; i++)
 7 //        {
 8 //            System.out.println(Color.values()[i]);
 9 //        }
10         for(Color c:Color.values())
11         {
12             System.out.println(c);
13         }
14 
15     }
16 
17 }
 1 package com.wsy.test;
 2 
 3 public class Test3 {
 4 
 5     public static void main(String[] args) {
 6         Color c1 = Color.BLUE;
 7         for(int i =0; i<Color.values().length; i++)
 8         {
 9             System.out.println(c1+"与"+Color.values()[i]+"的比较结果是"+c1.compareTo(Color.values()[i]));
10         }
11         //显示的结果证明如果相等的话返回0 如果想了解更多可以进入源代码查看
12         for(int i=0; i<Color.values().length; i++)
13         {
14             System.out.println("索引:"+Color.values()[i].ordinal()+" 值:"+Color.values()[i]);
15         }
16         //索引和数组下标一样 从0开始
17     }
18 
19 }
 1 package com.wsy.test;
 2 
 3 public class Test4 {
 4     public static void main(String[] args) {
 5         for(WeekDay day:WeekDay.values())
 6         {
 7             System.out.println(day+"=====>"+day.getDay());
 8         }
 9         WeekDay.printDay(1);
10     }
11 }
 1 package com.wsy.test;
 2 
 3 public enum WeekDay {
 4     MON("Monday"),TUE("Tuesday"),WED("Wednesday"),THU("Thursday"),FRI("Friday"),SAT("Saturday"),SUN("Sunday");
 5     private String day;
 6     private WeekDay(String day) 
 7     {
 8         this.day = day;
 9     }
10     public static void printDay(int day) 
11     {
12         switch(day)
13         {
14             case 1:
15                 System.out.println(WeekDay.MON);
16                 break;
17             case 2:
18                 System.out.println(WeekDay.TUE);
19                 break;
20             case 3:
21                 System.out.println(WeekDay.WED);
22                 break;
23             case 4:
24                 System.out.println(WeekDay.THU);
25                 break;
26             case 5:
27                 System.out.println(WeekDay.FRI);
28                 break;
29             case 6:
30                 System.out.println(WeekDay.SAT);
31                 break;
32             case 7:
33                 System.out.println(WeekDay.SUN);
34                 break;
35             default:
36                 System.out.println("error");
37                 break;
38         }
39     }
40     public String getDay() {
41         return day;
42     }
43     
44 }

猜你喜欢

转载自www.cnblogs.com/sucker/p/10770459.html