简体   繁体   English

用Java调用枚举

[英]Invoke enum in Java

Suppose I have a number of fields ( String or int ) in my Java enum, and I want to get a field value by its name , dynamically. 假设我的Java枚举中有多个字段( Stringint ),并且我想通过其名称动态获取字段值。

public enum Code {

    FIRST("valueForFirst"),
    SECOND("valueForSecond");
    // etc
}

Then I get the name of the field which I want: 然后,我得到了想要的字段名称:

String fieldName = getEnumFieldName(); // can be: "FIRST" or "SECOND" 
// now get "fieldName"'s value from Code

How can I do this ? 我怎样才能做到这一点 ?

You need to use Enum.valueOf(); 您需要使用Enum.valueOf(); such as: 如:

Code c = Code.valueOf(Code.class, fieldName);

If you are getting the name of your field from somewhere else in string form, you can use valueOf() method to get Enum instance.. But, first you would need to convert the string in all uppercase.. 如果要从其他地方以字符串形式获取字段名称,则可以使用valueOf()方法获取Enum实例。但是,首先,您需要将字符串全部转换为大写。

String fieldName = getEnumFieldName();
Code first = Code.valueOf(fieldName);
String value = first.getValue();

Go through this tutorial - http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html for more information on how to use Enums .. 通过本教程中去 - http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html有关如何使用的详细信息Enums ..

You can define your enum like this : 您可以这样定义您的枚举:

public enum Code {

    private String value;

    public Code(String value) {
       this.value = value;
    }

    public String getValue() {
       return value;
    }

    FIRST("valueForFirst"),
    SECOND("valueForSecond");
}

and then use it like this : 然后像这样使用它:

Code code = Code.FIRST;
String val = code.getValue();

or like this : 或像这样:

String key = "FIRST";
Code code = Code.valueOf(key);
String val = code.getValue();

If you want to get "FIRST" from a Code, just do 如果您想从代码中获取“第一”,只需执行

String name = code.name();

You can use the valueOf() method on the enum. 您可以在枚举上使用valueOf()方法。

String fieldName = "FIRST"; // or "SECOND" 
Code c = Code.valueOf(fieldName);

Here is the pattern which I use: 这是我使用的模式:

enum X {
    A("a"), B("b"), ...;

    private final static Map<String,X> MAP = new HashMap<String,X>();
    static {
        for( X elem: X.values() ) {
            if( null != MAP.put( elem.getValue(), elem ) ) {
                throw new IllegalArgumentException( "Duplicate value " + elem.getValue() );
            }
        }
    }

    private final String value;

    private X(String value) { this.value = value; }
    public String getValue() { return value; }

    // You may want to throw an error here if the map doesn't contain the key
    public static X byValue( String value ) { return MAP.get( value ); } 
}

It looks a bit odd to access the instances of the enum type in a static block inside of the enum declaration but this code works. enum声明内的static块中访问enum类型的实例看起来有些奇怪,但是此代码有效。

In your case, the could would look like so: 在您的情况下,可能看起来像这样:

String fieldName = Code.valueOf(Code.class).getValue();
    public enum Code {

        FIRST("valueForFirst"),
        SECOND("valueForSecond");

    }

    public class Test{

       Code c;

       public static void main(String[] args){

        Test t = new Test();

        String val = t.c.FIRST.getValue();



        }

  }

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

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