枚举类型中的构造方法、成员方法

1、枚举类型

1.1.含义:如果一个变量只有几种可能的值,则可以定义为枚举类型。

  例如enum Color{red,yellow,blue,white,black};声明了一个枚举类型

  然后可以用此类型来定义变量,如enum   Color     color1,color2

                                              枚举类型              枚举变量

1.2.特点

(1) 枚举变量和其它数值型不同,它们只限于花括号中制定的值之一。

(2)枚举中的每一个元素代表一个整数,默认0,1,2,3,。。

(3)赋值:red = 9;错。enum Color{red = 5,yellow = 4,blue = 4,white =2,black =1}正确。

(4)枚举元素的比较是按照其在初始化时指定的整数来进行比较的。

  在枚举类型中,可以添加构造方法,但是规定构造方法必须为private修饰符所修饰,举个例子说明构造方法的使用方法:

2、枚举类型中的成员方法

  枚举类型有很多的成员方法,可以将枚举类型看做是一个类,它集成于java.lang.Enum类。它具有以下方法: 

      

  下面以列子说用这几个方法的使用方法:

 1 public class ShowEnum {
 2     enum Constants2{
 3         A,B,C ; //可以没有分号,将常量放在枚举类型中
 4     }
 5     //定以比较枚举类型的方法,参数为枚举类型
 6     public static void compare(Constants2 c){
 7         //根据values()方法返回的数组做循环操作
 8         for (int i = 0; i < Constants2.values().length;i++){
 9             //将比较结果返回
10             System.out.println(c+"与"+Constants2.values()[i]+"的比较结果:"+c.compareTo(Constants2.values()[i]));
11         }
12     }
13     public static void main(String[] args) {
14         //调用compare方法
15         compare(Constants2.valueOf("B"));
16         System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
17         //循环有values返回的数组
18         for(int i = 0; i <Constants2.values().length;i++){
19             //将枚举成员变量打印
20             System.out.println("枚举成员变量为:"+Constants2.values()[i]);
21         }
22         System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
23         for(int i = 0;i <Constants2.values().length;i++){
24             //
25             System.out.println(Constants2.values()[i]+"在枚举类型中的索引位置为:"+Constants2.values()[i].ordinal());
26         }
27     }
28     
29 }

  运行结果:

  B与A的比较结果:1
  B与B的比较结果:0
  B与C的比较结果:-1
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  枚举成员变量为:A
  枚举成员变量为:B
  枚举成员变量为:C
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  A在枚举类型中的索引位置为:0
  B在枚举类型中的索引位置为:1
  C在枚举类型中的索引位置为:2

3、枚举类型中的构造方法

  例:

 1 public class EnumlndexTest {
 2     enum Constants2 {
 3         Constants_A("枚举成员A"),
 4         Constants_B("枚举成员B"), 
 5         Constants_C("枚举成员C"), 
 6         Constants_D(3);
 7         private String description;
 8         private int i = 4;
 9     
10         private Constants2() {
11 
12         }
13 
14         private Constants2(String description){
15            this.description =  description;
16        }
17 
18         private Constants2(int i) {
19             this.i = this.i + i;
20         }
21 
22         public String getDescription() {
23             return description;
24         }
25 
26         public int geti() {
27             return i;
28         }
29     
30   
31   }
32     public static void main(String[] args) {
33      for(int i = 0;i < Constants2.values().length;i++){
34          System.out.println(Constants2.values()[i]+"调用getDescription()方法为:"+Constants2.values()[i].getDescription());
35      }
36      System.out.println(Constants2.valueOf("Constants_D")+"调用geti() 方法为:"+Constants2.valueOf("Constants_D").geti());
37    }
38 }

  运行结果:

  Constants_A调用getDescription()方法为:枚举成员A
  Constants_B调用getDescription()方法为:枚举成员B
  Constants_C调用getDescription()方法为:枚举成员C
  Constants_D调用getDescription()方法为:null
  Constants_D调用geti() 方法为:7

  分析:将枚举类型中的构造方法设置为private,防止客户代码实例化一个枚举对象

  上述代码中的getDescription()方法也可以放在接口中,如下代码:

  在项目中创建d接口和枚举类型的AnyEnum类,在枚举类型AnyEnum类中实现带方法的接口,使每个枚举类型成员实现该接口中的方法。

 1 package com.lzw;
 2 import static java.lang.System.*;
 3 interface d {
 4     public String getDescription();
 5     
 6     public int getI();
 7 }
 8 
 9 public enum AnyEnum implements d {
10     Constants_A { // 可以在枚举类型成员内部设置方法
11         public String getDescription() {
12             return ("我是枚举成员A");
13         }
14         
15         public int getI() {
16             return i;
17         }
18     },
19     Constants_B {
20         public String getDescription() {
21             return ("我是枚举成员B");
22         }
23         
24         public int getI() {
25             return i;
26         }
27     },
28     Constants_C {
29         public String getDescription() {
30             return ("我是枚举成员C");
31         }
32         
33         public int getI() {
34             return i;
35         }
36     },
37     Constants_D {
38         public String getDescription() {
39             return ("我是枚举成员D");
40         }
41         
42         public int getI() {
43             return i;
44         }
45     };
46     private static int i = 5;
47     
48     public static void main(String[] args) {
49         for (int i = 0; i < AnyEnum.values().length; i++) {
50             out.println(AnyEnum.values()[i] + "调用getDescription()方法为:"
51                     + AnyEnum.values()[i].getDescription());
52             out.println(AnyEnum.values()[i] + "调用getI()方法为:"
53                     + AnyEnum.values()[i].getI());
54         }
55     }
56 }

  运行结果:

  Constants_A调用getDescription()方法为:我是枚举成员A
  Constants_A调用getI()方法为:5
  Constants_B调用getDescription()方法为:我是枚举成员B
  Constants_B调用getI()方法为:5
  Constants_C调用getDescription()方法为:我是枚举成员C
  Constants_C调用getI()方法为:5
  Constants_D调用getDescription()方法为:我是枚举成员D
  Constants_D调用getI()方法为:5

4、使用枚举类型设置常量

  通常在接口中设置常量,并且该常量不能被修改。因为在接口中定义常量时,该常量被修饰为static和final类型。在调用时不能检测参数的类型,而枚举类型定义的常量参数在调用时可以检测参数的类型。

  以下面例子说用枚举类型与与接口定义常量的区别:

 1 interface Constants { // 将常量放置在接口中
 2     public static final int Constants_A = 1;
 3     public static final int Constants_B = 12;
 4 }
 5 
 6 public class ConstantsTest {
 7     enum Constants2 { // 将常量放置在枚举类型中
 8         Constants_A, Constants_B
 9     }
10 
11     // 使用接口定义常量
12     public static void doit(int c) { // 定义一个方法,这里的参数为int型
13         switch (c) { // 根据常量的值做不同操作
14         case Constants.Constants_A:
15             System.out.println("doit() Constants_A");
16             break;
17         case Constants.Constants_B:
18             System.out.println("doit() Constants_B");
19             break;
20         }
21     }
22 
23     /** 定义一个方法,这里的参数为枚举类型对象 */
24     public static void doit2(Constants2 c) {
25         switch (c) { // 根据枚举类型对象做不同操作
26         case Constants_A:
27             System.out.println("doit2() Constants_A");
28             break;
29         case Constants_B:
30             System.out.println("doit2() Constants_B");
31             break;
32         }
33     }
34 
35     public static void main(String[] args) {
36         ConstantsTest.doit(Constants.Constants_A); // 使用接口中定义的常量
37         ConstantsTest.doit(12);
38         ConstantsTest.doit(Constants.Constants_B);
39         ConstantsTest.doit2(Constants2.Constants_A); // 使用枚举类型中的常量
40         ConstantsTest.doit2(Constants2.Constants_B); // 使用枚举类型中的常量
41         ConstantsTest.doit(2);
42         //ConstantsTest.doit2(2);             //  报错
43     }
44 }

  运行结果:

doit() Constants_A
doit() Constants_B
doit() Constants_B
doit2() Constants_A
doit2() Constants_B

  分析:在上述代码中,当用户调用doit()方法时,编译器不接受在接口中定义的常量参数,也不会报错,但调用doit2()方法时,任意传递的参数,编译器就会报错,因为这个方法只接受枚举类型的常量作为其参数。

原文地址:https://www.cnblogs.com/xyzyj/p/6185098.html