Java的编译器给enum自动添加了哪些专用方法?

来源:互联网 发布:php商城开源 编辑:程序博客网 时间:2024/06/05 04:21
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
        });
    }
}