简体   繁体   English

如何以编程方式定义Java枚举?

[英]How do I define a Java enum programmatically?

I need to define an Enum type in Java. 我需要在Java中定义一个Enum类型。 However, the values in the enum depends on a parameter. 但是,枚举中的值取决于参数。

public class EnumTest {

    private Language lang;

    public enum Language {English, Spanish, Chinese, German, Japanese;}

    public enum Entity {

        // ??? 
        entityA("student"),
        entityB("faculty"),
        entityC("staff");

        private String name;

        Entity(String name) {
            this.name = name;
        }
    }

    public EnumTest(Language lang) {
        this.lang = lang;
    }

    public static void main(String[] args) {
        EnumTest testA = new EnumTest(Langauge.English);
        EnumTest testB = new EnumTest(Language.Spanish);
    }
}

For example, the instantiation of entityA, entityB, entityC will not be "student", "faculty", and "staff", if the parameter is not Langauge.English. 例如,如果参数不是Langauge.English,则objectA,entityB,entityC的实例化将不是“ student”,“ faculty”和“ staff”。 It will be the corresponding words translated from English in other languages. 它将是用其他语言从英语翻译的相应单词。 So, the definition of the enum Entity depends on the parameter Lang. 因此,枚举实体的定义取决于参数Lang。

How can I achieve? 我该如何实现?

You could store the different translations within a field inside the enum. 您可以将不同的翻译存储在枚举内的字段中。

public enum Entity {

    EntityA("student", "translation1", "translation2", "translation3", "translation4"),
    EntityB("faculty", "translation1", "translation2", "translation3", "translation4"),
    EntityC("staff", "translation1", "translation2", "translation3", "translation4");

    public String[] names;

    private Entity(String english, String spanish, String chinese, String german, String japanese) {
        names = new String[]{english, spanish, chinese, german, japanese};
    }
}

You could then get the appropriate translation with a method like the following: 然后,您可以使用类似以下的方法来获取适当的翻译:

public String getTranslation(Entity entity, Language lang){
    return entity.names[lang.ordinal()]; // ordinal returns the id of the enum instance. The first declared has 0, the second has 1 and so on
}

So, the below code 所以,下面的代码

getTranslation(Entity.EntityA, Language.Spanish)

would return "translation1". 将返回“ translation1”。

The major advantage of enum types in Java is the ability for the compiler to know and have access to all instances at compile time . Java中的enum类型的主要优点是使编译器能够在编译时知道并访问所有实例 To define or modify enum values at runtime, even in tests, is to undermine Java enums and the guarantees and optimizations they support (eg serialization, VM-wide singleton properties, and well-defined ordinal and values[] ). 即使在测试中,在运行时定义或修改枚举值也将破坏Java枚举及其支持的保证和优化(例如,序列化,VM范围内的单例属性以及定义明确的ordinalvalues[] )。

A few alternatives you might consider: 您可以考虑以下几种选择:

  • Keep Entity as a enum, and shortly after runtime load a long-lived Map<Entity, String> that keeps the localized name. Entity保留为枚举,并在运行时加载后不久,保留长期名称的Map<Entity, String>保留本地化名称。

  • Limit access to the "Entity" constructor (to make the class instance-controlled) and then provide instances through a method or series of methods. 限制对“ Entity”构造函数的访问(使类受实例控制),然后通过一个或多个方法提供实例。

  • Have all localized strings on the enum take some sort of table instance (eg Properties or Map) that it uses to extract the right name based on an externally-kept key. 让枚举上的所有本地化字符串都采用某种表实例(例如,Properties或Map),该表实例用于根据外部保留的键提取正确的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM