A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 我手心里的宝 高级黑马   /  2013-4-9 09:12  /  2324 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

看到很多人纠结Enum与enum 与Enumberation详解 现总结了一下希望对大家有帮助
  1. 枚举类型是JDK5.0的新特征。Sun引进了一个全新的关键字enum来定义一个枚举类。下面就是一个典型枚举类型的定义:

  2. Java代码  
  3. 1.public enum Color{   
  4. 2.    RED,BLUE,BLACK,YELLOW,GREEN   
  5. 3.}  
  6. public enum Color{
  7.     RED,BLUE,BLACK,YELLOW,GREEN
  8. }显然,enum很像特殊的class,实际上enum声明定义的类型就是一个类。 而这些类都是类库中Enum类的子类(java.lang.Enum<E>)。它们继承了这个Enum中的许多有用的方法。我们对代码编译之后发现,编译器将enum类型单独编译成了一个字节码文件:Color.class。

  9. Color字节码代码  
  10. 1.final enum hr.test.Color {   
  11. 2.     
  12. 3. // 所有的枚举值都是类静态常量   
  13. 4. public static final enum hr.test.Color RED;   
  14. 5. public static final enum hr.test.Color BLUE;   
  15. 6. public static final enum hr.test.Color BLACK;   
  16. 7. public static final enum hr.test.Color YELLOW;   
  17. 8. public static final enum hr.test.Color GREEN;   
  18. 9.   
  19. 10.private static final synthetic hr.test.Color[] ENUM$VALUES;   
  20. 11.     
  21. 12.  // 初始化过程,对枚举类的所有枚举值对象进行第一次初始化   
  22. 13. static {   
  23. 14.       0  new hr.test.Color [1]   
  24. 15.      3  dup   
  25. 16.      4  ldc <String "RED"> [16] //把枚举值字符串"RED"压入操作数栈   
  26. 17.      6  iconst_0  // 把整型值0压入操作数栈   
  27. 18.      7  invokespecial hr.test.Color(java.lang.String, int) [17] //调用Color类的私有构造器创建Color对象RED   
  28. 19.     10  putstatic hr.test.Color.RED : hr.test.Color [21]  //将枚举对象赋给Color的静态常量RED。   
  29. 20.      .........  枚举对象BLUE等与上同   
  30. 21.    102  return   
  31. 22.};   
  32. 23.     
  33. 24.  // 私有构造器,外部不可能动态创建一个枚举类对象(也就是不可能动态创建一个枚举值)。   
  34. 25. private Color(java.lang.String arg0, int arg1){   
  35. 26.     // 调用父类Enum的受保护构造器创建一个枚举对象   
  36. 27.     3  invokespecial java.lang.Enum(java.lang.String, int) [38]   
  37. 28.};   
  38. 29.   
  39. 30. public static hr.test.Color[] values();   
  40. 31.     
  41. 32.   // 实现Enum类的抽象方法     
  42. 33.  public static hr.test.Color valueOf(java.lang.String arg0);   
  43. 34.}  
  44. final enum hr.test.Color {
  45.   
  46. // 所有的枚举值都是类静态常量
  47. public static final enum hr.test.Color RED;
  48. public static final enum hr.test.Color BLUE;
  49. public static final enum hr.test.Color BLACK;
  50. public static final enum hr.test.Color YELLOW;
  51. public static final enum hr.test.Color GREEN;

  52. private static final synthetic hr.test.Color[] ENUM$VALUES;
  53.   
  54.   // 初始化过程,对枚举类的所有枚举值对象进行第一次初始化
  55. static {
  56.        0  new hr.test.Color [1]
  57.       3  dup
  58.       4  ldc <String "RED"> [16] //把枚举值字符串"RED"压入操作数栈
  59.       6  iconst_0  // 把整型值0压入操作数栈
  60.       7  invokespecial hr.test.Color(java.lang.String, int) [17] //调用Color类的私有构造器创建Color对象RED
  61.      10  putstatic hr.test.Color.RED : hr.test.Color [21]  //将枚举对象赋给Color的静态常量RED。
  62.       .........  枚举对象BLUE等与上同
  63.     102  return
  64. };
  65.   
  66.   // 私有构造器,外部不可能动态创建一个枚举类对象(也就是不可能动态创建一个枚举值)。
  67. private Color(java.lang.String arg0, int arg1){
  68.      // 调用父类Enum的受保护构造器创建一个枚举对象
  69.      3  invokespecial java.lang.Enum(java.lang.String, int) [38]
  70. };

  71. public static hr.test.Color[] values();
  72.   
  73.    // 实现Enum类的抽象方法  
  74.   public static hr.test.Color valueOf(java.lang.String arg0);
  75. }  
  76. 下面我们就详细介绍enum定义的枚举类的特征及其用法。(后面均用Color举例)

  77. 1、Color枚举类就是class,而且是一个不可以被继承的final类。其枚举值(RED,BLUE...)都是Color类型的类静态常量, 我们可以通过下面的方式来得到Color枚举类的一个实例:
  78.                                                          Color c=Color.RED;
  79. 注意:这些枚举值都是public static final的,也就是我们经常所定义的常量方式,因此枚举类中的枚举值最好全部大写。

  80. 2、即然枚举类是class,当然在枚举类型中有构造器,方法和数据域。但是,枚举类的构造器有很大的不同:
  81.       (1) 构造器只是在构造枚举值的时候被调用。

  82. Java代码  
  83. 1.enum Color{   
  84. 2.                RED(255,0,0),BLUE(0,0,255),BLACK(0,0,0),YELLOW(255,255,0),GREEN(0,255,0);   
  85. 3.                //构造枚举值,比如RED(255,0,0)   
  86. 4.                private Color(int rv,int gv,int bv){   
  87. 5.                 this.redValue=rv;   
  88. 6.                 this.greenValue=gv;   
  89. 7.                 this.blueValue=bv;   
  90. 8.                }   
  91. 9.  
  92. 10.                public String toString(){  //覆盖了父类Enum的toString()   
  93. 11.                return super.toString()+"("+redValue+","+greenValue+","+blueValue+")";   
  94. 12.                }   
  95. 13.      
  96. 14.                private int redValue;  //自定义数据域,private为了封装。   
  97. 15.                private int greenValue;   
  98. 16.                private int blueValue;   
  99. 17. }  
  100. enum Color{
  101.                 RED(255,0,0),BLUE(0,0,255),BLACK(0,0,0),YELLOW(255,255,0),GREEN(0,255,0);
  102.                 //构造枚举值,比如RED(255,0,0)
  103.                 private Color(int rv,int gv,int bv){
  104.                  this.redValue=rv;
  105.                  this.greenValue=gv;
  106.                  this.blueValue=bv;
  107.                 }

  108.                 public String toString(){  //覆盖了父类Enum的toString()
  109.                 return super.toString()+"("+redValue+","+greenValue+","+blueValue+")";
  110.                 }
  111.    
  112.                 private int redValue;  //自定义数据域,private为了封装。
  113.                 private int greenValue;
  114.                 private int blueValue;
  115. }      (2) 构造器只能私有private,绝对不允许有public构造器。 这样可以保证外部代码无法新构造枚举类的实例。这也是完全符合情理的,因为我们知道枚举值是public static final的常量而已。 但枚举类的方法和数据域可以允许外部访问。

  116. Java代码  
  117. 1.public static void main(String args[])   
  118. 2.{   
  119. 3.        // Color colors=new Color(100,200,300);  //wrong   
  120. 4.           Color color=Color.RED;   
  121. 5.           System.out.println(color);  // 调用了toString()方法   
  122. 6.}     
  123. public static void main(String args[])
  124. {
  125.         // Color colors=new Color(100,200,300);  //wrong
  126.            Color color=Color.RED;
  127.            System.out.println(color);  // 调用了toString()方法
  128. }   
  129.   
  130. 3、所有枚举类都继承了Enum的方法,下面我们详细介绍这些方法。
  131.        (1)  ordinal()方法: 返回枚举值在枚举类种的顺序。这个顺序根据枚举值声明的顺序而定。
  132.                  Color.RED.ordinal();  //返回结果:0
  133.                  Color.BLUE.ordinal();  //返回结果:1
  134.        (2)  compareTo()方法: Enum实现了java.lang.Comparable接口,因此可以比较象与指定对象的顺序。Enum中的compareTo返回的是两个枚举值的顺序之差。当然,前提是两个枚举值必须属于同一个枚举类,否则会抛出ClassCastException()异常。(具体可见源代码)
  135.                  Color.RED.compareTo(Color.BLUE);  //返回结果 -1
  136.        (3)  values()方法: 静态方法,返回一个包含全部枚举值的数组。
  137.                  Color[] colors=Color.values();
  138.                  for(Color c:colors){
  139.                         System.out.print(c+",");
  140.                  }//返回结果:RED,BLUE,BLACK YELLOW,GREEN,
  141.        (4)  toString()方法: 返回枚举常量的名称。
  142.                  Color c=Color.RED;
  143.                  System.out.println(c);//返回结果: RED
  144.        (5)  valueOf()方法: 这个方法和toString方法是相对应的,返回带指定名称的指定枚举类型的枚举常量。
  145.                  Color.valueOf("BLUE");   //返回结果: Color.BLUE
  146.        (6)  equals()方法: 比较两个枚举类对象的引用。

  147. Java代码  
  148. 1.//JDK源代码:      
  149. 2.public final boolean equals(Object other) {   
  150. 3.        return this==other;   
  151. 4.}               
  152. //JDK源代码:   
  153. public final boolean equals(Object other) {
  154.         return this==other;
  155. }            
  156. 4、枚举类可以在switch语句中使用。

  157. Java代码  
  158. 1.Color color=Color.RED;   
  159. 2.switch(color){   
  160. 3.        case RED: System.out.println("it's red");break;   
  161. 4.        case BLUE: System.out.println("it's blue");break;   
  162. 5.        case BLACK: System.out.println("it's blue");break;   
  163. 6.}  
复制代码

1 个回复

倒序浏览
值得学习ing!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马