java - 如何正确扩展 Enum

标签 java android enums

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

类别:

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/

相关文章:

c++ - 不一致的完全限定枚举编译时行为

android - 如何在设备启动时启动 android 应用程序?

mysql - Cakephp 查询数据库中的枚举字段

java - Joda LocalDate - 还是瞬间?

java - DataInputStream readLong() 得到错误的值

java - 如何在没有cygwin的情况下在Windows上以分布式模式运行HBase?

java - JVM - 在正在运行的(非检测的) session 中获取类的实例

java - Surface::setBuffersDimensions 使用 android libdgdx 应用程序发送垃圾邮件

android - “Obsolete custom lint check” 来自 `androidx.appcompat.AppCompatIssueRegistry`,这需要更新的 API 级别

java - 唯一的枚举成员值