一、简介 枚举是一个被命名的整型常数的集合,用于声明一组带标识符的常数。枚举在曰常生活中很常见,例如一个人的性别只能是“男”或者“女”,一周的星期只能是 7 天中的一个等。类似这种当一个变量有几种固定可能的取值时,就可以将它定义为枚举类型。
在 JDK 1.5 之前没有枚举类型,那时候一般用接口常量来替代。而使用 Java 枚举类型 enum 可以更贴近地表示这种常量。
枚举类代表一组常量;   
枚举常量隐性声明为final static类型;   
每一个枚举常量代表了一个枚举类的对象;   
枚举类隐性实现了java.lang.Comparable接口和java.io.Serializable接口   
枚举类可以包涵多个构造函数 
 
声明枚举时必须使用 enum 关键字,然后定义枚举的名称、可访问性、基础类型和成员等。枚举声明的语法如下:
1 2 3 enum-modifiers enum enumname:enum-base {     enum-body, }
 
其中,
enum-modifiers 表示枚举的修饰符主要包括 public、private 和 internal; 
enumname 表示声明的枚举名称; 
enum-base 表示基础类型; 
enum-body 表示枚举的成员,它是枚举类型的命名常数。 
 
任意两个枚举成员不能具有相同的名称,且它的常数值必须在该枚举的基础类型的范围之内,多个枚举成员之间使用逗号分隔。  
二、默认枚举类 枚举类首先定义常量,其次才是属性、构造函数和方法。(注:常量名要大写)
1 2 3 4 5 6 7 8 9 10 11 12  public  enum  DefaultEnum {     DEMOENUM1, DEMOENUM2, DEMOENUM3; }public  class  client   {     public  static  void  main (String[] args)   {         DefaultEnum demoEnum = DefaultEnum.DEMOENUM1;         System.out.println(demoEnum);        } }
 
三、多值枚举对象 枚举对象可以包涵一个或多个值,值的类型可以任意基本类型。枚举对象的值在枚举类中定义后可定义setter方法修改,但是如非必要不要这么做,因为这会违背枚举对象常量的本质。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 public  enum  MutiValueEnum {     DEMOENUM1(1 , "hello" ),     DEMOENUM2(2 , "enum" );               private  int  id;     private  String value;               MutiValueEnum(int  id, String value) {         this .id = id;         this .value = value;     }               public  int  getId ()   { return  this .id; }               public  String getValue ()   { return  this .value; } }public  class  client   {     public  static  void  main (String[] args)   {         MutiValueEnum mutiValueEnum = MutiValueEnum.DEMOENUM1;                  System.out.println(mutiValueEnum);         System.out.println(mutiValueEnum.getId());         System.out.println(mutiValueEnum.getValue());     } }
 
四、属性和方法 Java 中的每一个枚举都继承自 java.lang.Enum 类。当定义一个枚举类型时,每一个枚举类型成员都可以看作是 Enum 类的实例,这些枚举成员默认都被 final、public, static 修饰,当使用枚举类型成员时,直接使用枚举名称调用成员即可。  
枚举类里的属性和方法是属于枚举常量的,所以枚举常量可以调用这些方法。我们可以在枚举类定义其他我们所需要的方法。其实,枚举类同正常类有许多相似之处,对于变量和方法,我们可以按照正常类里的属性和方法去使用,只不过我们不能通过new来创建对象,而是枚举类自行创建。注:枚举类里不能有抽象方法。  
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public  enum  TestAttributeAndMethodOfEnum {     DEMOENUM;          private  int  id;     private  String value;     private  boolean  flag;               …               public  void  TestMethod1 ()   { … }     public  void  TestMethod2 ()   { … } }public  class  Client   {     public  static  void  main (String[] args)   {         TestAttributeAndMethodOfEnum testEnum = TestAttributeAndMethodOfEnum.DEMOENUM;                  testEnum.setId(1 );         testEnum.TestMethod1();         …     } }
 
五、构造函数 同正常类一样,枚举类可以拥有多个构造函数。构造函数的参数要同枚举常量的值的数量相一致。并且枚举类的构造函数的可见性只能设为包可见或是私有,其次也不能通过new来创建枚举对象。由枚举类的使用类负责枚举对象的构建。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 enum Color {      RED,      GREEN("light"),     BLUE(255,23,223);           private String level;     private int r,g,b;          //默认无参构造,在此可省略     //Color() { }          //对应一个值的GREEN常量     Color(String level) {         this.level = level;     }     //对应三个值的BLUE常量     Color(int r, int g, int b) {         this.r = r;         this.g = g;         this.b = b;     } } public class Test {     // Driver method      public static void main(String[] args) {          Color c1 = Color.RED;          Color c2 = Color.GREEN;         Color c3 = Color.BLUE;         System.out.println(c1);          …     }  } //在上述例子中,枚举类会被内部的转换为 class Color{     public static final Color RED = new Color();     public static final Color GREEN = new Color("light");     public static final Color BLUE = new Color(255, 23, 223); }
 
六、重要方法 
values() 返回枚举类的所有常量;   
ordinal() 返回枚举常量的序号   
valueOf(String name) 返回名为name的枚举常量,如果存在。 
compareTo() 比较两个枚举成员在定义时的顺序 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 enum  Color {      RED, GREEN, BLUE;  } public  class  Test   {      public  static  void  main (String[] args)   {          Color arr[] = Color.values();          for  (Color col : arr) {              System.out.println(col + " at index "  + col.ordinal());          }          System.out.println(Color.valueOf("RED" ));                  System.out.println(Color.valueOf("WHITE" ));      }  }