什么是最好的方法来使用存储在Enum字符串字面量的值? 例如:

public enum Modes {
    some-really-long-string,
    mode1,
    mode2,
    mode3
}

然后我就可以用《风尚》了。将其字符串表示形式返回为Mode1。而不必一直调用Mode.mode1.toString()。


当前回答

你不能。我认为你有四个选择。这四家公司都提供了解决方案,但方法略有不同。

选项一:在枚举上使用内置name()。如果您不需要任何特殊的命名格式,这是非常好的。

    String name = Modes.mode1.name(); // Returns the name of this enum constant, exactly as declared in its enum declaration.

选项二:如果你想要更多的控制,给你的枚举添加覆盖属性

public enum Modes {
    mode1 ("Fancy Mode 1"),
    mode2 ("Fancy Mode 2"),
    mode3 ("Fancy Mode 3");

    private final String name;       

    private Modes(String s) {
        name = s;
    }

    public boolean equalsName(String otherName) {
        // (otherName == null) check is not needed because name.equals(null) returns false 
        return name.equals(otherName);
    }

    public String toString() {
       return this.name;
    }
}

选项三:使用静态韵母代替枚举:

public final class Modes {

    public static final String MODE_1 = "Fancy Mode 1";
    public static final String MODE_2 = "Fancy Mode 2";
    public static final String MODE_3 = "Fancy Mode 3";

    private Modes() { }
}

选项四:接口的每个字段都是public、static和final:

public interface Modes {

    String MODE_1 = "Fancy Mode 1";
    String MODE_2 = "Fancy Mode 2";
    String MODE_3 = "Fancy Mode 3";  
}

其他回答

我发现这个更容易防止输入错误:

public enum Modes {
    some-really-long-string,
    mode1,
    mode2,
    mode3;

    String str;

    Modes(){
        this.str = super.name();
    }

    @Override
    @NonNull
    public String toString() {
        return str;
    }

然而,当你需要在log/println上使用String时,或者当java自动编译toString()方法时,这可能会起作用,但在这样的代码行中->

// sample method that require (string,value)
intent.putExtra(Modes.mode1 ,shareElement.getMode()); // java error
// first argument enum does not return value

相反,如上所述,你仍然需要扩展枚举,并在这些情况下使用.name():

intent.putExtra(Modes.mode1.name() ,shareElement.getMode()); 

正如Benny Neugebauer提到的,您可以覆盖toString()。然而,取而代之的是为每个枚举字段重写toString,我更喜欢这样:

public enum Country{
    SPAIN("España"),
    ITALY("Italia"),
    PORTUGAL("Portugal");


    private String value;

    Country(final String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    @Override
    public String toString() {
        return this.getValue();
    }
}

您还可以添加一个静态方法来检索所有字段,打印所有字段,等等。 只需调用getValue来获取与每个Enum项关联的字符串

你可以使用Mode.mode1.name(),但是你通常不需要这样做。

Mode mode =
System.out.println("The mode is "+mode);

你不能。我认为你有四个选择。这四家公司都提供了解决方案,但方法略有不同。

选项一:在枚举上使用内置name()。如果您不需要任何特殊的命名格式,这是非常好的。

    String name = Modes.mode1.name(); // Returns the name of this enum constant, exactly as declared in its enum declaration.

选项二:如果你想要更多的控制,给你的枚举添加覆盖属性

public enum Modes {
    mode1 ("Fancy Mode 1"),
    mode2 ("Fancy Mode 2"),
    mode3 ("Fancy Mode 3");

    private final String name;       

    private Modes(String s) {
        name = s;
    }

    public boolean equalsName(String otherName) {
        // (otherName == null) check is not needed because name.equals(null) returns false 
        return name.equals(otherName);
    }

    public String toString() {
       return this.name;
    }
}

选项三:使用静态韵母代替枚举:

public final class Modes {

    public static final String MODE_1 = "Fancy Mode 1";
    public static final String MODE_2 = "Fancy Mode 2";
    public static final String MODE_3 = "Fancy Mode 3";

    private Modes() { }
}

选项四:接口的每个字段都是public、static和final:

public interface Modes {

    String MODE_1 = "Fancy Mode 1";
    String MODE_2 = "Fancy Mode 2";
    String MODE_3 = "Fancy Mode 3";  
}
package com.common.test;

public  enum Days {


    monday(1,"Monday"),tuesday(2,"Tuesday"),wednesday(3,"Wednesday"),
    thrusday(4,"Thrusday"),friday(5,"Friday"),saturday(6,"Saturday"),sunday(7,"Sunday");

    private int id;
    private String desc;


    Days(int id,String desc){
        this.id=id;
        this.desc=desc;
    }

    public static String getDay(int id){

        for (Days day : Days.values()) {
            if (day.getId() == id) {
                return day.getDesc();
            }
        }
        return null;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }



};