什么是最好的方法来使用存储在Enum字符串字面量的值? 例如:
public enum Modes {
some-really-long-string,
mode1,
mode2,
mode3
}
然后我就可以用《风尚》了。将其字符串表示形式返回为Mode1。而不必一直调用Mode.mode1.toString()。
什么是最好的方法来使用存储在Enum字符串字面量的值? 例如:
public enum Modes {
some-really-long-string,
mode1,
mode2,
mode3
}
然后我就可以用《风尚》了。将其字符串表示形式返回为Mode1。而不必一直调用Mode.mode1.toString()。
当前回答
您可以为每个枚举值重写toString()方法。
例子:
public enum Country {
DE {
@Override
public String toString() {
return "Germany";
}
},
IT {
@Override
public String toString() {
return "Italy";
}
},
US {
@Override
public String toString() {
return "United States";
}
}
}
用法:
public static void main(String[] args) {
System.out.println(Country.DE); // Germany
System.out.println(Country.IT); // Italy
System.out.println(Country.US); // United States
}
其他回答
我的解决方案!
import java.util.HashMap;
import java.util.Map;
public enum MapEnumSample {
Mustang("One of the fastest cars in the world!"),
Mercedes("One of the most beautiful cars in the world!"),
Ferrari("Ferrari or Mercedes, which one is the best?");
private final String description;
private static Map<String, String> enumMap;
private MapEnumSample(String description) {
this.description = description;
}
public String getEnumValue() {
return description;
}
public static String getEnumKey(String name) {
if (enumMap == null) {
initializeMap();
}
return enumMap.get(name);
}
private static Map<String, String> initializeMap() {
enumMap = new HashMap<String, String>();
for (MapEnumSample access : MapEnumSample.values()) {
enumMap.put(access.getEnumValue(), access.toString());
}
return enumMap;
}
public static void main(String[] args) {
// getting value from Description
System.out.println(MapEnumSample.getEnumKey("One of the fastest cars in the world!"));
// getting value from Constant
System.out.println(MapEnumSample.Mustang.getEnumValue());
System.out.println(MapEnumSample.getEnumKey("One of the most beautiful cars in the world!"));
System.out.println(MapEnumSample.Mercedes.getEnumValue());
// doesnt exist in Enum
System.out.println("Mustang or Mercedes, which one is the best?");
System.out.println(MapEnumSample.getEnumKey("Mustang or Mercedes, which one is the best?") == null ? "I don't know!" : "I believe that "
+ MapEnumSample.getEnumKey("Ferrari or Mustang, which one is the best?") + " is the best!.");
// exists in Enum
System.out.println("Ferrari or Mercedes, wich one is the best?");
System.out.println(MapEnumSample.getEnumKey("Ferrari or Mercedes, which one is the best?") == null ? "I don't know!" : "I believe that "
+ MapEnumSample.getEnumKey("Ferrari or Mercedes, which one is the best?") + " is the best!");
}
}
据我所知,唯一的办法就是
Mode.mode1.name();
然而,如果你真的需要这样做,你可以这样做:
public enum Modes {
mode1 ("Mode1"),
mode2 ("Mode2"),
mode3 ("Mode3");
private String name;
private Modes(String s) {
name = s;
}
}
正如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项关联的字符串
你可以试试这个:
public enum Modes {
some-really-long-string,
mode1,
mode2,
mode3;
public String toString(){
switch(this) {
case some-really-long-string:
return "some-really-long-string";
case mode2:
return "mode2";
default: return "undefined";
}
}
}
经过多次尝试,我终于想出了这个解决办法
public static enum Operation {
Addition, Subtraction, Multiplication, Division,;
public String getUserFriendlyString() {
if (this==Addition) {
return " + ";
} else if (this==Subtraction) {
return " - ";
} else if (this==Multiplication) {
return " * ";
} else if (this==Division) {
return " / ";
}
return "undefined";
}
}