Java的編譯器給enum自動添加了哪些專用方法?

from Java Tutorial:

Java programming language enum types are much more powerful than their counterparts in other languages. The enum declaration defines a class (called an enum type). The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared. This method is commonly used in combination with the for-each construct to iterate over the values of an enum type. For example, this code from the Planet class example below iterates over all the planets in the solar system.

請問:
除values()方法外,編譯器還給這個enum添加了哪些方法?
簡單例子 :
enum Test{
    Spring,Summer,Autumn,Winter
}
反編譯的結果:
// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: [url]http://www.kpdus.com/jad.html[/url]
// Decompiler options: packimports(3)
// Source File Name:   Test.java


final class Test extends Enum
{

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

    public static Test valueOf(String s)
    {
        return (Test)Enum.valueOf(Test, s);
    }

    private Test(String s, int i)
    {
        super(s, i);
    }

    public static final Test Spring;
    public static final Test Summer;
    public static final Test Autumn;
    public static final Test Winter;
    private static final Test $VALUES[];

    static
    {
        Spring = new Test("Spring", 0);
        Summer = new Test("Summer", 1);
        Autumn = new Test("Autumn", 2);
        Winter = new Test("Winter", 3);
        $VALUES = (new Test[] {
            Spring, Summer, Autumn, Winter
        });
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章