gpt4 book ai didi

java - 如何正确扩展 Enum

转载 作者:行者123 更新时间:2023-12-01 18:31:40 24 4
gpt4 key购买 nike

当我使用此类时,它显示一些错误。

类别:

public static final class ScreenOrientation extends Enum
{

private static final ScreenOrientation ENUM$VALUES[];
public static final ScreenOrientation LANDSCAPE_FIXED;
public static final ScreenOrientation LANDSCAPE_SENSOR;
public static final ScreenOrientation PORTRAIT_FIXED;
public static final ScreenOrientation PORTRAIT_SENSOR;

public static ScreenOrientation valueOf(String s)
{
return (ScreenOrientation)Enum.valueOf(org/andengine/engine/options/EngineOptions$ScreenOrientation, s);
}

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

static
{
LANDSCAPE_FIXED = new ScreenOrientation("LANDSCAPE_FIXED", 0);
LANDSCAPE_SENSOR = new ScreenOrientation("LANDSCAPE_SENSOR", 1);
PORTRAIT_FIXED = new ScreenOrientation("PORTRAIT_FIXED", 2);
PORTRAIT_SENSOR = new ScreenOrientation("PORTRAIT_SENSOR", 3);
ScreenOrientation ascreenorientation[] = new ScreenOrientation[4];
ascreenorientation[0] = LANDSCAPE_FIXED;
ascreenorientation[1] = LANDSCAPE_SENSOR;
ascreenorientation[2] = PORTRAIT_FIXED;
ascreenorientation[3] = PORTRAIT_SENSOR;
ENUM$VALUES = ascreenorientation;
}

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

扩展Enum时,它显示类型q可能不会显式子类化Enum错误。

如何使用这些字段创建枚举?
如何修改此类以像 Enum 一样使用?

最佳答案

public enum ScreenOrientation {

LANDSCAPE_FIXED("LANDSCAPE_FIXED", 0);
// other values

private final String s;
private final int i;

private ScreenOrientation(String s, int i) {
this.s = s;
this.i = i;
}

// getters for s and i

@Override
public String toString() {
return this.getS();
}

public static ScreenOrientation fromS(String s) {
for( ScreenOrientation so : values())
if(so.getS().equalsIgnoreCase(s)) {
return so;
}
throw new IllegalArgumentException();
}
}

不确定 s 和 i 等同于什么,所以给它们指定更有意义的名称。另外,按照 Javadocs 中的建议重写 toString 方法:

http://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html#name()

关于java - 如何正确扩展 Enum,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23899328/

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