java枚举使用 总结

补充几点:

1.枚举对象是可以用 == 比较。

2.

TestEnum3反编译结果:

F:	reeTestsrc	est>javap TestEnum3*
Compiled from "TestEnum3.java"
final class test.TestEnum3$1 extends test.TestEnum3 {
  test.TestEnum3$1(java.lang.String, int);
  void discription();
}
Compiled from "TestEnum3.java"
final class test.TestEnum3$2 extends test.TestEnum3 {
  test.TestEnum3$2(java.lang.String, int);
  void discription();
}
Compiled from "TestEnum3.java"
final class test.TestEnum3$3 extends test.TestEnum3 {
  test.TestEnum3$3(java.lang.String, int);
  void discription();
}
Compiled from "TestEnum3.java"
public abstract class test.TestEnum3 extends java.lang.Enum<test.TestEnum3> {
  public static final test.TestEnum3 GREEN;
  public static final test.TestEnum3 RED;
  public static final test.TestEnum3 YELLOW;
  public static test.TestEnum3[] values();
  public static test.TestEnum3 valueOf(java.lang.String);
  abstract void discription();
  public static void main(java.lang.String[]);
  test.TestEnum3(java.lang.String, int, test.TestEnum3$1);
  static {};
}

TestEnum5反编译结果:

public final class test.TestEnum5 extends java.lang.Enum<test.TestEnum5> {
  public static final test.TestEnum5 RED;
  public static final test.TestEnum5 GREEN;
  public static final test.TestEnum5 YELLOW;
  public static test.TestEnum5[] values();
  public static test.TestEnum5 valueOf(java.lang.String);
  public java.lang.String getColor();
  public int getIndex();
  public java.lang.String toString();
  public static java.lang.String getInstance(int);
  public static void main(java.lang.String[]);
  static {};
}
 1 package test;
 2 
 3 enum Colors {
 4     RED, YELLOW, WHITE, GREEN
 5 }
 6 
 7 /**
 8  * 
 9  * @Function:TestEnum
10  * @Description: 枚举做常量
11  * @author 
12  * @date :2018/04/10上午11:00:09
13  *
14  */
15 public class TestEnum {
16 
17     public static void main(String[] args) {
18         for (Colors string : Colors.values()) {
19             System.out.println("color:" + string);
20         }
21         System.out.println("====================");
22         for (Colors string : Colors.values()) {
23             System.out.println(string + " ordinal " + string.ordinal());
24         }
25     }
26 }
27 /*
28  * color:RED 
29  * color:YELLOW 
30  * color:WHITE 
31  * color:GREEN 
32  * ==================== 
33  * RED ordinal 0 
34  * YELLOW ordinal 1 
35  * WHITE ordinal 2 
36  * GREEN ordinal 3
37  */
 1 package test;
 2 
 3 enum Color {
 4     GREEN, RED, YELLOW, White
 5 }
 6 
 7 /**
 8  * 
 9  * @Function:TestEnum
10  * @Description: 枚举作用于switch
11  * @author 
12  * @date :2018/04/10上午11:00:09
13  *
14  */
15 public class TestEnum1 {
16     Color color = Color.White;
17 
18     public static void main(String[] args) {
19 
20         TestEnum1 testEnum = new TestEnum1();
21         System.out.println(testEnum.change());
22 
23     }
24 
25     public Color change() {
26         switch (color) {
27 
28         case GREEN:
29             color = Color.GREEN;
30             break;
31         case RED:
32             color = Color.RED;
33             break;
34         case YELLOW:
35             color = Color.YELLOW;
36             break;
37         case White:
38             color = Color.White;
39             break;
40         }
41         return color;
42     }
43 }
44 /*
45  * White
46  */
 1 package test;
 2 
 3 /**
 4  * 
 5  * @Function:TestEnum
 6  * @Description: 枚举添加方法
 7  * @author 
 8  * @date :2018/04/10上午11:00:09
 9  *
10  */
11 public enum TestEnum2 {
12     RED("红色", 1), YELLOW("黄色", 2), WHITE("白色", 3);
13 
14     private String color;
15     private int index;
16 
17     // 私有构造方法
18     private TestEnum2(String color, int index) {
19         this.color = color;
20         this.index = index;
21     }
22 
23     // 通过index获取颜色的静态方法
24     public static String getColor(int index) {
25         for (TestEnum2 colorObj : TestEnum2.values()) {
26             if (colorObj.getIndex() == index) {
27                 return colorObj.getColor();
28             }
29         }
30         return null;
31     }
32 
33     public String getColor() {
34         return color;
35     }
36 
37     public int getIndex() {
38         return index;
39     }
40 
41     public static void main(String[] args) {
42         System.out.println(getColor(2));
43     }
44 }
45 /*
46  * 黄色
47  */
 1 package test;
 2 
 3 /**
 4  * 
 5  * @Function:TestEnum3
 6  * @Description:枚举多态性,添加抽象方法
 7  * @author 
 8  * @date :2018/04/10上午11:04:27 编译javac **.java ,反编译 javap **
 9  *       反编译后发现,"枚举常量"继承了TestEnum3重写了抽象方法,此类java.lang.Enum抽象类子类,反编译后此类为抽象类
10  */
11 public enum TestEnum3 {
12     GREEN {
13         void discription() {
14             System.out.println("绿灯行!");
15         }
16     },
17     RED {
18         void discription() {
19             System.out.println("红灯停!");
20         }
21     },
22     YELLOW {
23         void discription() {
24             System.out.println("黄灯等一等!");
25         }
26     };
27     abstract void discription();
28 
29     public static void main(String[] args) {
30         for (TestEnum3 s : TestEnum3.values()) {
31             s.discription();
32         }
33     }
34 }
35 /*
36  * 绿灯行!
37  * 红灯停!
38  * 黄灯等一等!
39  */
 1 package test;
 2 
 3 /**
 4  * 
 5  * @Function:TestEnum4
 6  * @Description: 利用构造器为实例添加描述
 7  * @author 
 8  * @date :2018/04/10下午12:06:54
 9  *
10  */
11 public enum TestEnum4 {
12     RED("红色"), GREEN("绿色"), YELLOW("黄色"); //实为类对象的实例引用
13 
14     public String colorFlag;
15 
16     // 我的理解是,枚举常量内的参数(描述)需要跟这个私有的构造器参数一致
17     private TestEnum4(String flag) {
18         this.colorFlag = flag;
19     }
20 
21     public String getColorFlag() {
22         return colorFlag;
23     }
24 
25     public static void main(String[] args) {
26         for (TestEnum4 s : TestEnum4.values()) {
27             System.out.println(s.getColorFlag());
28         }
29     }
30 
31 }
32 /*
33  * 红色
34  * 绿色
35  * 黄色
36  */
 1 package test;
 2 
 3 /**
 4  * 
 5  * @Function:TestEnum5
 6  * @Description: 覆盖枚举的方法
 7  * @author 
 8  * @date :2018/04/10下午12:06:54 ================================在本类不能实例化(不知原因)
 9  */
10 public enum TestEnum5 {
11     RED("红色", 1), GREEN("绿色", 2), YELLOW("黄色", 3);
12 
13     private String color;
14     private int index;
15 
16     // 我的理解是,枚举常量内的参数(描述)需要跟这个私有的构造器参数一致
17     private TestEnum5(String color, int index) {
18         this.color = color;
19         this.index = index;
20     }
21 
22     public String getColor() {
23         return color;
24     }
25 
26     public int getIndex() {
27         return index;
28     }
29 
30     @Override
31     public String toString() {
32         return this.index + " _color:" + this.color;
33     }
34 
35     public static String getInstance(int index) {
36         for (TestEnum5 s : TestEnum5.values()) {
37             if (s.getIndex() == index) {
38                 // 入果去掉重写toString方法,则返回RED
39                 return s.toString();
40             }
41         }
42         return null;
43     }
44 
45     public static void main(String[] args) {
46         System.out.println(getInstance(2));
47     }
48 }
49 /*
50  * 2 _color:绿色
51  */
 1 package test;
 2 
 3 interface Color6 {
 4     void print();
 5 
 6     String getColor();
 7 }
 8 
 9 /**
10  * 
11  * @Function:TestEnum6
12  * @Description: 枚举实现接口
13  * @author 
14  * @date :2018/04/10下午1:44:46
15  *
16  */
17 public enum TestEnum6 implements Color6 {
18     RED("红色", 1), GREEN("绿色", 2), YELLOW("黄色", 3);
19 
20     private String color;
21     private int index;
22 
23     private TestEnum6(String color, int index) {
24         this.color = color;
25         this.index = index;
26     }
27 
28     @Override
29     public void print() {
30         //补充:this.name 可以返回此枚举常量名称
31         System.out.println(this.index + " color:" + this.color); 
32     }
33 
34     @Override
35     public String getColor() {
36         return this.color;
37     }
38 
39     public static void main(String[] args) {
40         for (TestEnum6 t : TestEnum6.values()) {
41             t.print();
42         }
43     }
44 }
45 /*
46  *  1 color:红色
47  *    2 color:绿色
48  *    3 color:黄色
49 */
 1 package test;
 2 
 3 interface Color7 {
 4 
 5     enum colors1 implements Color7 {
 6         YELLOW_FRUIT, GREEN_FRUIT, RED_FRUIT
 7     }
 8 
 9     enum colors2 implements Color7 {
10         YELLOW, GREEN, RED
11     }
12 }
13 
14 /**
15  * 
16  * @Function:TestEnum6
17  * @Description: 使用接口组织枚举
18  * @author 
19  * @date :2018/04/10下午1:44:46
20  *
21  */
22 public class TestEnum7 implements Color7 {
23     public static void testImplInstance() {
24         for (Color7.colors1 c1 : Color7.colors1.values()) {
25             System.out.println(c1.name());
26         }
27         System.out.println("==================");
28         for (Color7 c2 : Color7.colors2.values()) {
29             System.out.println(c2);
30         }
31         System.out.println("==================");
32         // 搞个实现接口,来组织枚举,简单讲,就是分类吧。如果大量使用枚举的话,这么干,在写代码的时候,就很方便调用啦。
33         // 还有就是个“多态”的功能吧,
34         Color7 color = Color7.colors2.YELLOW;
35         System.out.println(color);
36     }
37 
38     public static void main(String[] args) {
39         testImplInstance();
40     }
41 }
42 /*
43  *    YELLOW_FRUIT
44  *    GREEN_FRUIT
45  *    RED_FRUIT
46  *    ==================
47  *    YELLOW
48  *    GREEN
49  *    RED
50  *    ==================
51  *    YELLOW
52  */
原文地址:https://www.cnblogs.com/tmftmb/p/8778970.html