Enum 原理

From : http://blog.csdn.net/ZangXT/archive/2008/10/29/3174741.aspx

 

java5增加了enum,其实对enum的处理很简单,编译器增加了一些功能而以。
看代码:

  1. public   enum  Season{
  2.     Spring,Summer,Autumn,Winter
  3. }

javap -c Season
就可以看出来具体的处理了,不过直接反编译更容易理解,使用jad处理的结果。

  1. // Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
  2. // Jad home page: http://www.kpdus.com/jad.html
  3. // Decompiler options: packimports(3) 
  4. // Source File Name:   Season.java
  5. public   final   class  Season  extends  Enum
  6. {
  7.      public   static   final  Season[] values()
  8.     {
  9.          return  (Season[])$VALUES.clone();
  10.     }
  11.      public   static  Season valueOf(String s)
  12.     {
  13.          return  (Season)Enum.valueOf(Season, s);
  14.     }
  15.      private  Season(String s,  int  i)
  16.     {
  17.          super (s, i);
  18.     }
  19.      public   static   final  Season Spring;
  20.      public   static   final  Season Summer;
  21.      public   static   final  Season Autumn;
  22.      public   static   final  Season Winter;
  23.      private   static   final  Season $VALUES[];
  24.      static  
  25.     {
  26.         Spring =  new  Season( "Spring" 0 );
  27.         Summer =  new  Season( "Summer" 1 );
  28.         Autumn =  new  Season( "Autumn" 2 );
  29.         Winter =  new  Season( "Winter" 3 );
  30.         $VALUES = ( new  Season[] {
  31.             Spring, Summer, Autumn, Winter
  32.         });
  33.     }
  34. }


一个从java.lang.Enum<E>继承下来的final类,没有什么特殊的地方。使用的时候
Season season = Season.Spring;
不过就是直接访问了Season类中定义的静态常量Spring而已。

其实从java 5开始加入的一些新的语法特性,都是编译器增加了处理,而对于jvm而言,基本不需要什么变化。

 

 

例子:

 

package NewPractice;

public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY
}

===========================

 

package NewPractice;

public class EnumTest {
    Day day;

    public EnumTest(Day day) {
        this.day = day;
    }

    public void tellItLikeItIs() {
        switch (day) {
        case MONDAY:
            System.out.println("Mondays are bad.");
            break;

        case FRIDAY:
            System.out.println("Fridays are better.");
            break;

        case SATURDAY:
        case SUNDAY:
            System.out.println("Weekends are best.");
            break;

        default:
            System.out.println("Midweek days are so-so.");
            break;
        }
    }

    public static void main(String[] args) {
        EnumTest firstDay = new EnumTest(Day.MONDAY);
        firstDay.tellItLikeItIs();
        EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
        thirdDay.tellItLikeItIs();
        EnumTest fifthDay = new EnumTest(Day.FRIDAY);
        fifthDay.tellItLikeItIs();
        EnumTest sixthDay = new EnumTest(Day.SATURDAY);
        sixthDay.tellItLikeItIs();
        EnumTest seventhDay = new EnumTest(Day.SUNDAY);
        seventhDay.tellItLikeItIs();

    }
}

 

输出:

 

Mondays are bad.
Midweek days are so-so.
Fridays are better.
Weekends are best.
Weekends are best.

 

另外一个例子:

http://java.sun.com/docs/books/tutorial/java/javaOO/enum.html

你可能感兴趣的:(Enum 原理)