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()方法時,任意傳遞的參數,編譯器就會報錯,因為這個方法只接受枚舉類型的常量作為其參數。
