什么是最好的方法来使用存储在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()。
当前回答
每个枚举都有一个name()和valueOf(String)方法。前者返回enum的字符串名称,后者给出名称为该字符串的enum值。这就是你要找的吗?
String name = Modes.mode1.name();
Modes mode = Modes.valueOf(name);
Enum本身也有一个静态valueOf(Class, String),所以你也可以使用:
Modes mode = Enum.valueOf(Modes.class, name);
其他回答
mode1.name()或String.valueOf(mode1)。恐怕没有比这更好的了
正如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项关联的字符串
对于我的枚举,我真的不喜欢把它们分配给每个1个字符串。这就是我如何在枚举上实现toString()方法。
enum Animal
{
DOG, CAT, BIRD;
public String toString(){
switch (this) {
case DOG: return "Dog";
case CAT: return "Cat";
case BIRD: return "Bird";
}
return null;
}
}
你不能。我认为你有四个选择。这四家公司都提供了解决方案,但方法略有不同。
选项一:在枚举上使用内置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";
}
我的解决方案!
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!");
}
}