Java枚举类型

大家有没有考虑过Java枚举类型在Java中是如何实现的?接下来通过一个小例子来看看Java是如何实现枚举类型的。
这是一个Week的枚举类型。

public enum Week {
    Sunday(0),
    Monday(1),
    Tuesday(2),
    Wednesday(3),
    Thursday(4),
    Friday(5),
    Saturday(6);

    private int digit;

    Week(int digit) {
        this.digit = digit;
    }

    public Week valueOf(int digit) {
        for (Week week : values()) {
            if (week.getDigit() == digit) {
                return week;
            }
        }
        return null;
    }

    public int getDigit() {
        return digit;
    }

    public void setDigit(int digit) {
        this.digit = digit;
    }

}

枚举类型定义Week好了之后,通过
javac Week.java
javap -c Week.class 命令查看Week的字节码

Compiled from "Week.java"
public final class Week extends java.lang.Enum {
  public static final Week Sunday;

  public static final Week Monday;

  public static final Week Tuesday;

  public static final Week Wednesday;

  public static final Week Thursday;

  public static final Week Friday;

  public static final Week Saturday;

  static {};
    Code:
       0: new           #4                  // class Week
       3: dup
       4: ldc           #10                 // String Sunday
       6: iconst_0
       7: iconst_0
       8: invokespecial #11                 // Method "":(Ljava/lang/String;II)V
      11: putstatic     #12                 // Field Sunday:LWeek;
      14: new           #4                  // class Week
      17: dup
      18: ldc           #13                 // String Monday
      20: iconst_1
      21: iconst_1
      22: invokespecial #11                 // Method "":(Ljava/lang/String;II)V
      25: putstatic     #14                 // Field Monday:LWeek;
      28: new           #4                  // class Week
      31: dup
      32: ldc           #15                 // String Tuesday
      34: iconst_2
      35: iconst_2
      36: invokespecial #11                 // Method "":(Ljava/lang/String;II)V
      39: putstatic     #16                 // Field Tuesday:LWeek;
      42: new           #4                  // class Week
      45: dup
      46: ldc           #17                 // String Wednesday
      48: iconst_3
      49: iconst_3
      50: invokespecial #11                 // Method "":(Ljava/lang/String;II)V
      53: putstatic     #18                 // Field Wednesday:LWeek;
      56: new           #4                  // class Week
      59: dup
      60: ldc           #19                 // String Thursday
      62: iconst_4
      63: iconst_4
      64: invokespecial #11                 // Method "":(Ljava/lang/String;II)V
      67: putstatic     #20                 // Field Thursday:LWeek;
      70: new           #4                  // class Week
      73: dup
      74: ldc           #21                 // String Friday
      76: iconst_5
      77: iconst_5
      78: invokespecial #11                 // Method "":(Ljava/lang/String;II)V
      81: putstatic     #22                 // Field Friday:LWeek;
      84: new           #4                  // class Week
      87: dup
      88: ldc           #23                 // String Saturday
      90: bipush        6
      92: bipush        6
      94: invokespecial #11                 // Method "":(Ljava/lang/String;II)V
      97: putstatic     #24                 // Field Saturday:LWeek;
     100: bipush        7
     102: anewarray     #4                  // class Week
     105: dup
     106: iconst_0
     107: getstatic     #12                 // Field Sunday:LWeek;
     110: aastore
     111: dup
     112: iconst_1
     113: getstatic     #14                 // Field Monday:LWeek;
     116: aastore
     117: dup
     118: iconst_2
     119: getstatic     #16                 // Field Tuesday:LWeek;
     122: aastore
     123: dup
     124: iconst_3
     125: getstatic     #18                 // Field Wednesday:LWeek;
     128: aastore
     129: dup
     130: iconst_4
     131: getstatic     #20                 // Field Thursday:LWeek;
     134: aastore
     135: dup
     136: iconst_5
     137: getstatic     #22                 // Field Friday:LWeek;
     140: aastore
     141: dup
     142: bipush        6
     144: getstatic     #24                 // Field Saturday:LWeek;
     147: aastore
     148: putstatic     #1                  // Field $VALUES:[LWeek;
     151: return
}

通过字节码,可以看出枚举类型的内部实现其实是一个继承了Java Enum类,然后将所有枚举值声明成static final的Week类类型,并通过静态初始化方式初始化了这些值。

你可能感兴趣的:(Java)