枚举在Java中如何内部表示?

前端之家收集整理的这篇文章主要介绍了枚举在Java中如何内部表示?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Java枚举是类.它们被编译为类.

下面的例子如何编译?
什么是“班级”?
什么是确切的类代码?我想要实际的Java代码.

public enum Ordinals {
    FIRST("st"),SECOND("nd"),THIRD("rd");
    private String notation;
    private Ordinals(String notation) {
        this.notation = notation;
    }
    public String getNotation() {
        return notation;
    }
}

解决方法

每个枚举类被编译为一个类,它是java.lang.Enum的子类.每个枚举常数成为该类内的静态最终常数.然后,以声明的顺序创建一个数组$VALUES,其中包含所有枚举常量.

您可以使用命令javap -p -c Ordinals(在编译的.class文件)上反汇编代码,以了解详细信息.

Compiled from "Ordinals.java"
public final class Ordinals extends java.lang.Enum<Ordinals> {
  public static final Ordinals FIRST;

  public static final Ordinals SECOND;

  public static final Ordinals THIRD;

  private java.lang.String notation; // your custom field

  private static final Ordinals[] $VALUES; // all enum constants

  public static Ordinals[] values(); // every enum class has this static method
    Code:
       0: getstatic     #1                  // Field $VALUES:[LOrdinals;
       3: invokevirtual #2                  // Method "[LOrdinals;".clone:()Ljava/lang/Object;
       6: checkcast     #3                  // class "[LOrdinals;"
       9: areturn       

  public static Ordinals valueOf(java.lang.String); // every enum class has this static method
    Code:
       0: ldc_w         #4                  // class Ordinals
       3: aload_0       
       4: invokestatic  #5                  // Method java/lang/Enum.valueOf:(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Enum;
       7: checkcast     #4                  // class Ordinals
      10: areturn       

  private Ordinals(java.lang.String);
    Code:
       0: aload_0       
       1: aload_1       
       2: iload_2       
       3: invokespecial #6                  // Method java/lang/Enum."<init>":(Ljava/lang/String;I)V
       6: aload_0       
       7: aload_3       
       8: putfield      #7                  // Field notation:Ljava/lang/String;
      11: return        

  public java.lang.String getNotation();
    Code:
       0: aload_0       
       1: getfield      #7                  // Field notation:Ljava/lang/String;
       4: areturn       

  static {}; // fills the $VALUES array and initializes the static fields corresponding to the enum constants
    Code:
       0: new           #4                  // class Ordinals
       3: dup           
       4: ldc           #8                  // String FIRST
       6: iconst_0      
       7: ldc           #9                  // String st
       9: invokespecial #10                 // Method "<init>":(Ljava/lang/String;ILjava/lang/String;)V
      12: putstatic     #11                 // Field FIRST:LOrdinals;
      15: new           #4                  // class Ordinals
      18: dup           
      19: ldc           #12                 // String SECOND
      21: iconst_1      
      22: ldc           #13                 // String nd
      24: invokespecial #10                 // Method "<init>":(Ljava/lang/String;ILjava/lang/String;)V
      27: putstatic     #14                 // Field SECOND:LOrdinals;
      30: new           #4                  // class Ordinals
      33: dup           
      34: ldc           #15                 // String THIRD
      36: iconst_2      
      37: ldc           #16                 // String rd
      39: invokespecial #10                 // Method "<init>":(Ljava/lang/String;ILjava/lang/String;)V
      42: putstatic     #17                 // Field THIRD:LOrdinals;
      45: iconst_3      
      46: anewarray     #4                  // class Ordinals
      49: dup           
      50: iconst_0      
      51: getstatic     #11                 // Field FIRST:LOrdinals;
      54: aastore       
      55: dup           
      56: iconst_1      
      57: getstatic     #14                 // Field SECOND:LOrdinals;
      60: aastore       
      61: dup           
      62: iconst_2      
      63: getstatic     #17                 // Field THIRD:LOrdinals;
      66: aastore       
      67: putstatic     #1                  // Field $VALUES:[LOrdinals;
      70: return        
}

这将转换回Java

public final class Ordinals extends java.lang.Enum<Ordinals> {
  public static final Ordinals FIRST;

  public static final Ordinals SECOND;

  public static final Ordinals THIRD;

  private String notation;

  private static final Ordinals[] $VALUES;

  public static Ordinals[] values() {
      return $VALUES.clone();
  }

  public static Ordinals valueOf(String name) {
      return (Ordinals) Enum.valueOf(Ordinals.class,name);
  }

  private Ordinals(String name,int ordinal,String notation) {
      super(name,ordinal);
      this.notation = notation
  }

  static {
      FIRST = new Ordinals("FIRST","st");
      SECOND = new Ordinals("SECOND",1,"nd");
      THIRD = new Ordinals("THIRD",2,"rd");
      Ordinals[] $VALUES = new Ordinals[3];
      $VALUES[0] = FIRST;
      $VALUES[1] = SECOND;
      $VALUES[2] = THIRD;
      Ordinals.$VALUES = $VALUES;
  }
}

类版本完全不相关 – 这取决于您使用的Java编译器的版本(或编译器上的显式设置,以强制其编译为较旧的Java版本).

原文链接:https://www.f2er.com/java/124953.html

猜你在找的Java相关文章