像往常一样,@JonSkeets 的直觉胜出。编译他提供的代码:
public enum Fruits {
APPLE, BANANA;
}
jad
然后用产量反编译:
public final class Fruits extends Enum
{
public static Fruits[] values()
{
return (Fruits[])$VALUES.clone();
}
public static Fruits valueOf(String s)
{
return (Fruits)Enum.valueOf(Fruits, s);
}
private Fruits(String s, int i)
{
super(s, i);
}
public static final Fruits APPLE;
public static final Fruits BANANA;
private static final Fruits $VALUES[];
static
{
APPLE = new Fruits("APPLE", 0);
BANANA = new Fruits("BANANA", 1);
$VALUES = (new Fruits[] {
APPLE, BANANA
});
}
}
完整的示例说明了当您声明枚举时编译器为您所做的所有工作。请注意,正如@MarkoTopolnik 指出的那样,您不能自己执行此操作,仅仅是因为编译器不允许这样做。