【2019春招准备:27.Enum枚举类型】

public enum Coloe {
	RED, GREEN, BLUE;

	public static void main(String[] args) {
		for (Enum color : Coloe.values()) {
			System.out.println(color);
		}
	}
}

class反编译之后

import java.io.PrintStream;

public final class Coloe extends Enum
{

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

    public static void main(String args[])
    {
        Coloe acoloe[];
        int j = (acoloe = values()).length;
        for(int i = 0; i < j; i++)
        {
            Enum color = acoloe[i];
            System.out.println(color);
        }

    }

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

    public static Coloe valueOf(String s)
    {
        return (Coloe)Enum.valueOf(q99999_Test/Coloe, s);
    }

    public static final Coloe RED;
    public static final Coloe GREEN;
    public static final Coloe BLUE;
    private static final Coloe ENUM$VALUES[];

    static 
    {
        RED = new Coloe("RED", 0);
        GREEN = new Coloe("GREEN", 1);
        BLUE = new Coloe("BLUE", 2);
        ENUM$VALUES = (new Coloe[] {
            RED, GREEN, BLUE
        });
    }
}

Enum 变成 final class
枚举成员变成final static 成员
编译的时候会新增一个静态代码块(执行是在类装载-初始化的步骤):创建final class的数组,并初始化里面每一个成员变量(名称和序号),并且提供一个返回该数组的方法。

Enum.枚举成员.ordinal可以获取序号
compareTo

你可能感兴趣的:(面试,Java)