java枚举类

应用

enum Type{
    BOOK,NOTEBOOK
}

实现原理

编译器会生成一个继承enum接口的Type类,对编译的Type.class使用jad反编译可以得到

final class Type extends Enum
{

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

    public static Type[] values()
    {
        Type atype[];
        int i;
        Type atype1[];
        System.arraycopy(atype = ENUM$VALUES, 0, atype1 = new Type[i = atype.length], 0, i);
        return atype1;
    }

    public static Type valueOf(String s)
    {
        return (Type)Enum.valueOf(liupeng/cn/Algorithm/thread/Type, s);
    }

    public static final Type BOOK;
    public static final Type NOTEBOOK;
    private static final Type ENUM$VALUES[];

    static 
    {
        BOOK = new Type("BOOK", 0);
        NOTEBOOK = new Type("NOTEBOOK", 1);
        ENUM$VALUES = (new Type[] {
            BOOK, NOTEBOOK
        });
    }
}

你可能感兴趣的:(java枚举类)