1. 程式人生 > >Java - 枚舉

Java - 枚舉

兩種 方式 div string 是否 sig 進行 body 實現

Java中,枚舉的其中用法:

1、定義常量:

技術分享圖片
 1 /**
 2  * 枚舉第一種用法:常量;替代之前的定義在類中的常量
 3  */
 4 package enum_;
 5 
 6 public enum Color1
 7 {
 8     RED,
 9     YELLOW,
10     BLUE,
11     GREEN  // 最後一個加逗號,加分號,不加,都沒報錯
12 }
13 
14 class EnumDemo01
15 {
16     public static void main(String[] args)
17     {
18         Color1 color = Color1.RED;
19 Color1 color2 = Color1.RED; 20 Color1 color3 = Color1.YELLOW; 21 22 // 結果返回false 23 System.out.println("RED".equals(color)); 24 // 結果返回true 25 System.out.println("RED".equals(color.toString())); 26 27 // 結果返回false 28 System.out.println("RED".equals(Color1.RED));
29 // 結果返回true 30 System.out.println("RED".equals(Color1.RED.toString())); 31 32 // enum類型轉為String類型的兩種方式 33 // 1.調用toString()方法,返回此枚舉常量的名稱; 34 String red = color.toString(); 35 System.out.println("RED".equals(red)); // 結果返回true 36 // 2.調用name()方法,返回此枚舉常量的名稱;
37 String name = color.name(); 38 System.out.println("RED".equals(name)); // 結果返回true 39 40 // 使用 == 或 equals() 比較兩個枚舉類是否相等,效果是一樣的 41 // 1.使用 == 進行比較 42 System.out.println(color.equals(color2)); 43 System.out.println(color == color2); 44 // 2.使用 equals() 進行比較 45 System.out.println(color.equals(color3)); 46 System.out.println(color == color3); 47 } 48 }
枚舉第一種用法:常量;替代之前的定義在類中的常量

2、在 switch 中使用:

技術分享圖片
 1 /**
 2  * 枚舉的第二種用法:放在switch中
 3  */
 4 package enum_;
 5 
 6 public enum Color2
 7 {
 8     RED, GREEN, YELLOW, BLUE
 9 }
10 
11 class EnumDemo02
12 {
13     public static void main(String[] args)
14     {
15         Color2 signal = Color2.BLUE;
16         
17         switch(signal)
18         {
19             case RED:
20                 System.out.println("紅燈亮...");
21                 break;
22             case GREEN:
23                 System.out.println("綠燈亮...");
24                 break;
25             case YELLOW:
26                 System.out.println("黃燈亮...");
27                 break;
28             default:
29                 System.out.println("燈壞了,不亮了...");
30         }
31     }
32 }
枚舉的第二種用法:放在switch中

3、添加屬性和方法:

技術分享圖片
 1 /**
 2  * 枚舉的第三種用法:向枚舉中添加屬性和方法
 3  */
 4 package enum_;
 5 
 6 public enum Color3
 7 {
 8     // 如果自定義自己的方法,那麽必須在enum實例序列的最後添加一個分號;而且 Java 要求必須先定義 enum 實例;
 9     // 下面實例是依據構造方法來實現這個寫法
10     RED("紅色",1), YELLOW("黃色",2), BLUE("藍色",3), GREEN("綠色",4);
11     
12     // 成員變量  
13     private String name;
14     private Integer index;
15     
16     // 構造方法
17     // 構造方法的訪問限定符只能是private或默認不寫
18     private Color3(String name, Integer index)
19     {
20         this.name = name;
21         this.index = index;
22     }
23     
24     // 普通方法  
25     public String getColorName(Integer index)
26     {
27         for(Color3 c : Color3.values())
28         {
29             if(c.getIndex() == index)
30                 return c.getName();
31         }
32         
33         return null;
34     }
35 
36     // get、set 方法 
37     public String getName()
38     {
39         return name;
40     }
41     public void setName(String name)
42     {
43         this.name = name;
44     }
45     public Integer getIndex()
46     {
47         return index;
48     }
49     public void setIndex(Integer index)
50     {
51         this.index = index;
52     }
53     
54 }
55 
56 class EnumDemo03
57 {
58     public static void main(String[] args)
59     {
60         Color3 c = Color3.RED;
61         System.out.println(c.getName());
62         System.out.println(c.getIndex());
63         System.out.println(c.getColorName(2));
64     }
65 }
枚舉的第三種用法:向枚舉中添加屬性和方法

4、覆蓋枚舉的方法:

技術分享圖片
 1 /**
 2  * 枚舉第四種用法:覆蓋枚舉的方法
 3  */
 4 package enum_;
 5 
 6 public enum Color4
 7 {
 8     RED("紅色",1), YELLOW("黃色",2), BLUE("藍色",3), GREEN("綠色",4);
 9     
10     private String name;
11     private Integer index;
12     
13     private Color4(String name, Integer index)
14     {
15         this.name = name;
16         this.index = index;
17     }
18 
19     public String getName()
20     {
21         return name;
22     }
23     public void setName(String name)
24     {
25         this.name = name;
26     }
27     public Integer getIndex()
28     {
29         return index;
30     }
31     public void setIndex(Integer index)
32     {
33         this.index = index;
34     }
35     
36     // 重寫toString()方法
37     @Override
38     public String toString()
39     {
40         return "name:" + name + ",index:" + index;
41     }
42     
43 }
44 
45 class EnumDemo04
46 {
47     public static void main(String[] args)
48     {
49         Color4 c = Color4.RED;
50         System.out.println(c.getName());
51         System.out.println(c.getIndex());
52         System.out.println(c.toString());
53     }
54 }
枚舉第四種用法:覆蓋枚舉的方法

5、實現接口:

技術分享圖片
 1 /**
 2  * 枚舉第五種用法:實現接口
 3  */
 4 package enum_;
 5 
 6 public enum Color5 implements Behaviour
 7 {
 8     RED("紅色",1), YELLOW("黃色",2), BLUE("藍色",3), GREEN("綠色",4);
 9     
10     private String name;
11     private Integer index;
12     
13     private Color5(String name, Integer index)
14     {
15         this.name = name;
16         this.index = index;
17     }
18 
19     @Override
20     public void printColorInfo()
21     {
22         System.out.println("name:" + name + ",index:" + index);
23     }
24 
25     public String getName()
26     {
27         return name;
28     }
29     public void setName(String name)
30     {
31         this.name = name;
32     }
33     public Integer getIndex()
34     {
35         return index;
36     }
37     public void setIndex(Integer index)
38     {
39         this.index = index;
40     }
41     
42     @Override
43     public String toString()
44     {
45         return "name:" + name + ",index:" + index;
46     }
47 
48 }
49 
50 interface Behaviour
51 {
52     void printColorInfo();
53 }
54 
55 class EnumDemo05
56 {
57     public static void main(String[] args)
58     {
59         Color5 c = Color5.RED;
60         System.out.println(c.toString());
61         c.printColorInfo();
62     }
63 }
枚舉第五種用法:實現接口

6、使用接口組織枚舉:

技術分享圖片
 1 /**
 2  * 枚舉第六種用法:使用接口組織枚舉
 3  */
 4 package enum_;
 5 
 6 // 還必須導包
 7 import enum_.Color6.Color6_1;
 8 import enum_.Color6.Color6_2;
 9 
10 public interface Color6
11 {
12     enum Color6_1
13     {
14         RED, YELLOW
15     }
16     
17     enum Color6_2
18     {
19         BLUE, GREEN
20     }
21 }
22 
23 class EnumDeno06
24 {
25     public static void main(String[] args)
26     {
27         Color6_1 c1 = Color6.Color6_1.RED;
28         Color6_2 c2 = Color6.Color6_2.BLUE;
29         
30         System.out.println(c1.name());
31         System.out.println(c2.name());
32     }
33 }
枚舉第六種用法:使用接口組織枚舉

7、枚舉集合的使用:
java.util.EnumSet和java.util.EnumMap是兩個枚舉集合;EnumSet保證集合中的元素不重復;
EnumMap中的key是enum類型,而value則可以是任意類型;關於這個兩個集合的使用就不在這裏贅述,可以參考JDK文檔;

Java - 枚舉