Java 可扩展枚举

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/221892/
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-29 11:29:23  来源:igfitidea点击:

Java extendable enumeration

javadesign-patternsenumsenumeration

提问by Milhous

Is there a way to write an enumeration that can be extended. I have several methods that I would like to always have available for my enumerations. For example I use an enumeration for my database fields. I include the actual field name in the database.

有没有办法编写可以扩展的枚举。我有几种方法,我希望始终可用于我的枚举。例如,我对我的数据库字段使用枚举。我在数据库中包含了实际的字段名称。

public enum ORDERFIELDS
        {
            OrderID("Order_ID");
            private String FieldName;

            private ORDERFIELDS(String fname)
                {
                    this.FieldName = fname;
                }

            public String getFieldName()
                {
                    return FieldName;
                }
        } 

回答by Guido

All enums implicitly extend java.lang.Enum. Since Java does not support multiple inheritance, an enum cannot extend anything else.

所有枚举都隐式扩展java.lang.Enum。由于 Java 不支持多重继承,因此枚举不能扩展任何其他内容。

回答by Dónal

If I understand correctly, what you'd like to do is something like this:

如果我理解正确,你想做的是这样的:

public abstract class DatabaseField {
    private String fieldName;

    private DatabaseField(String fieldName) {
        this.fieldName = fieldName;
    }

    public String getFieldName() {
        return fieldName;
    }
}

Then define your enum to extend this class. However, unfortunately an enum cannot extend a class, but it can implement an interface, so the best you can do at the moment is define an interface which includes the getFieldName() method and have all your enums implement this interface.

然后定义你的枚举来扩展这个类。然而,不幸的是,枚举不能扩展一个类,但它可以实现一个接口,所以目前你能做的最好的事情就是定义一个包含 getFieldName() 方法的接口,并让你的所有枚举实现这个接口。

However, this means that you'll have to duplicate the implementation of this method (and any others) in all your enums. There are some suggestions in this questionabout ways to minimise this duplication.

但是,这意味着您必须在所有枚举中复制此方法(以及任何其他方法)的实现。在这个问题中有一些关于减少这种重复的方法的建议。

回答by Alex Miller

Abstract enums are potentially very useful (and currently not allowed). But a proposal and prototype exists if you'd like to lobby someone in Sun to add it:

抽象枚举可能非常有用(目前是不允许的)。但是,如果您想游说 Sun 中的某个人添加它,则存在提案和原型:

http://freddy33.blogspot.com/2007/11/abstract-enum-ricky-carlson-way.html

http://freddy33.blogspot.com/2007/11/abstract-enum-ricky-carlson-way.html

Sun RFE:

孙RFE:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6570766

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6570766

回答by Guemundur Bjarni

Enums can implement interfaces but not extend since when compiled they translate to a java.lang.Enum.

枚举可以实现接口但不能扩展,因为在编译时它们会转换为 java.lang.Enum。

回答by Guemundur Bjarni

For a throwback to the pre-Java 5 days, take a look at Item 21, Chapter 5,Effective Java by Josh Bloch. He talks about extending "enums" by adding values, but perhaps you could use some of the techniques to add a new method?

要回顾 Java 之前的 5 天,请查看Josh Bloch 撰写的第 21 章第 5 章,Effective Java。他谈到通过添加值来扩展“枚举”,但也许您可以使用一些技术来添加新方法?

回答by Aidos

Hand craft the enum in a mechanism similar to that defined in Josh Bloch's Effective Java.

以类似于 Josh Bloch 的 Effective Java 中定义的机制手工制作枚举。

I would add that if you need to "extend" the enum, then perhaps an enum isn't the construct you are after. They are meant to be static constants IMHO.

我想补充一点,如果您需要“扩展”枚举,那么枚举可能不是您所追求的构造。恕我直言,它们是静态常量。