如何在 Java 中扩展 Enum?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13856297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:12:23  来源:igfitidea点击:

How to extends Enum in Java?

javaenums

提问by Devu Soman

I have a class from sample project.But when I am using this class it shows some errors.The class is given below.

我有一个来自示例项目的类。但是当我使用这个类时,它显示了一些错误。下面给出了这个类。

public  class q extends Enum
{

    private int i = -1;
    private String s = null;

    private q(String s, int i)
    {
       // super(s, i);
        this.s = s;
        this.i  = i;
    }

    public static q valueOf(String s)
    {
        return (q)Enum.valueOf(q.class, s);
    }

    public static q[] values()
    {
        return (q[])a.clone();
    }

    public static final q ANDROID_VERSION;
    public static final q APP_VERSION_CODE;
    public static final q APP_VERSION_NAME;
    public static final q AVAILABLE_MEM_SIZE;
    private static final q a[];

    static 
    {
        APP_VERSION_CODE = new q("APP_VERSION_CODE", 1);
        APP_VERSION_NAME = new q("APP_VERSION_NAME", 2);
        ANDROID_VERSION = new q("ANDROID_VERSION", 6);
        AVAILABLE_MEM_SIZE = new q("AVAILABLE_MEM_SIZE", 11);
        q aq[] = new q[34];
        aq[0] = APP_VERSION_CODE;
        aq[1] = ANDROID_VERSION;
        aq[2] = APP_VERSION_NAME;
        aq[3] = AVAILABLE_MEM_SIZE;
        a = aq;
    }

}

When extending Enum it shows "The type q may not subclass Enum explicitly" error.How can i create an enum using these fields?

扩展 Enum 时,它显示“类型 q 可能不会显式地将 Enum子类化”错误。如何使用这些字段创建枚举?

How can i modify this class to use like enum(ie,I want to use the default enum methods like ordinal(),valueOf(...) etc.. on this.)?

我如何修改这个类以使用 enum(即,我想使用默认的 enum 方法,如 ordinal()、valueOf(...) 等。)?

回答by thaussma

You cannot extend from Enum. You have to declare an Enumclass like:

您不能从Enum. 你必须声明一个Enum类,如:

public enum Q {
    TYPE1, TYPE2, TYPE3;
}

And you also cannot instantiate an enum class directly. Each type of your enum class is instantiated exactly once by the virtual machine.

而且您也不能直接实例化枚举类。每种类型的枚举类都被虚拟机实例化一次。

public class MyClass {

    public enum MyEnum{
        TYPE1("Name", 9,1,100000), TYPE2("Name2", 10, 1, 200000);

        private final int androidVersion;
        private final int appVersionCode;
        private final int availableMemSize;
        private final String appVersionName;

        private MyEnum(String appVersionName, int androidVersion, int appVersionCode, int availableMemSize) {
            this.androidVersion = androidVersion;
            this.appVersionCode = appVersionCode;
            this.availableMemSize = availableMemSize;
            this.appVersionName = appVersionName;
        }
    }
    MyEnum mType = MyEnum.TYPE1;
}

回答by atamanroman

Enums basically boil down to something like this:

枚举基本上归结为这样的事情:

public Enum Q
{
    TYPE1, TYPE2, TYPE3:
}

// is roughy translated to 

public final class Q
{
    private Q() {}

    public static final Q TYPE1 = new Q();
    public static final Q TYPE2 = new Q();
    public static final Q TYPE3 = new Q();
}

There is more you can do, but this should explain why you can not instantiate Q.

您还可以做更多的事情,但这应该可以解释为什么您不能实例化Q.

回答by Kumar Vivek Mitra

-Think of the enumkeyword as syntatic sugar. It sets up classes with normal inheritance trees for you, but will not allow you to extendjava.lang.Enum.

-enum关键字视为语法糖。它为您设置具有普通继承树的类,但不允许您扩展java.lang.Enum.

Eg:

例如:

public enum TEST {  
    ONE, TWO, THREE;
}