gpt4 book ai didi

java 枚举类型变得有点困惑

转载 作者:行者123 更新时间:2023-12-01 23:17:52 25 4
gpt4 key购买 nike

嗨,我这些天正在练习 java 枚举,并接受了调试这些代码的任务,我发现其中一个非常具有挑战性,它是

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$VALUES[] 的工作 谁能向我解释一下这段代码的工作原理以及一些掌握枚举的好教程谢谢。

最佳答案

你做错了。

如果你想声明一个枚举,请这样做:

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

关于java 枚举类型变得有点困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20951615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com