gpt4 book ai didi

java - 枚举是如何在 Java 中实现的?

转载 作者:搜寻专家 更新时间:2023-10-30 19:56:53 25 4
gpt4 key购买 nike

我刚刚意识到它们比使用常量类更强大和实用……但我很想知道它们是如何在幕后实现的。它们的工作方式似乎很像静态方法或静态常量,因为您只需导入“拥有”枚举的文件,就可以使用 enumName.valueName 来引用它们。在线的 Javadocs 似乎暗示它们是一个 class 但是根据需要有一个“未命名”的类似乎很奇怪。 (至少在 Java 中...)

最佳答案

我相信枚举的每个实例都是枚举的匿名最终子类。

反编译:

public enum Color {
r(0xff0000), g(0x00ff00), b(0x0000ff);
Color(int rgb) {
this.rgb=rgb;
}
final int rgb;
}

你可以看到正在创建的实例:

D:\home\ray\dev\indigoapps\so8032067enumimpl\bin>javap -c Color
Compiled from "Color.java"
public final class Color extends java.lang.Enum{
public static final Color r;

public static final Color g;

public static final Color b;

final int rgb;

static {};
Code:
0: new #1; //class Color
3: dup
4: ldc #16; //String r
6: iconst_0
7: ldc #17; //int 16711680
9: invokespecial #18; //Method "<init>":(Ljava/lang/String;II)V
12: putstatic #22; //Field r:LColor;
15: new #1; //class Color
18: dup
19: ldc #24; //String g
21: iconst_1
22: ldc #25; //int 65280
24: invokespecial #18; //Method "<init>":(Ljava/lang/String;II)V
27: putstatic #26; //Field g:LColor;
30: new #1; //class Color
33: dup
34: ldc #28; //String b
36: iconst_2
37: sipush 255
40: invokespecial #18; //Method "<init>":(Ljava/lang/String;II)V
43: putstatic #29; //Field b:LColor;
46: iconst_3
47: anewarray #1; //class Color
50: dup
51: iconst_0
52: getstatic #22; //Field r:LColor;
55: aastore
56: dup
57: iconst_1
58: getstatic #26; //Field g:LColor;
61: aastore
62: dup
63: iconst_2
64: getstatic #29; //Field b:LColor;
67: aastore
68: putstatic #31; //Field ENUM$VALUES:[LColor;
71: return

public static Color[] values();
Code:
0: getstatic #31; //Field ENUM$VALUES:[LColor;
3: dup
4: astore_0
5: iconst_0
6: aload_0
7: arraylength
8: dup
9: istore_1
10: anewarray #1; //class Color
13: dup
14: astore_2
15: iconst_0
16: iload_1
17: invokestatic #43; //Method java/lang/System.arraycopy:(Ljava/lang/Obj
ect;ILjava/lang/Object;II)V
20: aload_2
21: areturn

public static Color valueOf(java.lang.String);
Code:
0: ldc #1; //class Color
2: aload_0
3: invokestatic #51; //Method java/lang/Enum.valueOf:(Ljava/lang/Class;L
java/lang/String;)Ljava/lang/Enum;
6: checkcast #1; //class Color
9: areturn

}

关于java - 枚举是如何在 Java 中实现的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8032067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com