java enum类型有点混淆

时间:2014-01-06 14:02:23

标签: java enums

嗨,我这几天正在练习java enum,并且已经完成了调试这些代码的任务,我发现这里的一个非常具有挑战性

public final class ParkingAttributes extends Enum
    {

    public static final ParkingAttributes BIKE;
    public static final ParkingAttributes CAR;
    public static final ParkingAttributes CYCLE;
    private static final ParkingAttributes ENUM$VALUES[];

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

    public static ParkingAttributes valueOf(String s)
    {
        return (ParkingAttributes)Enum.valueOf(com/tilzmatictech/mobile/navigation/delhimetronavigator/metro/ParkingAttributes, s);
    }

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

    static 
    {
        CAR = new ParkingAttributes("CAR", 0);
        BIKE = new ParkingAttributes("BIKE", 1);
        CYCLE = new ParkingAttributes("CYCLE", 2);
        ParkingAttributes aparkingattributes[] = new ParkingAttributes[3];
        aparkingattributes[0] = CAR;
        aparkingattributes[1] = BIKE;
        aparkingattributes[2] = CYCLE;
        ENUM$VALUES = aparkingattributes;
    }
    }

有一件事,我知道枚举是一个最后的类,不能扩展我在这里找不到的地方是enum被定义和ENUM $ VALUES []的工作  任何人都可以解释我这个代码的工作和一些很好的教程来掌握枚举 感谢。

1 个答案:

答案 0 :(得分:3)

你做错了。

如果要声明枚举,请按以下步骤操作:

public enum ParkingAttributes { //Implicitly a final class that extends Enum
  BIKE, CAR, CYCLE//Implicitly static (but not final!) instances of ParkingAttributes.
}