使用存储在Enum中做为字符串文字的值的最佳方法是什么? 例如: java
public enum Modes { some-really-long-string, mode1, mode2, mode3 }
而后,稍后我能够使用Mode.mode1
将其字符串表示形式返回为mode1
。 无需继续调用Mode.mode1.toString()
。 ide
您能够为每一个枚举值覆盖toString()
方法。 this
例: spa
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"; } } }
用法: code
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 }
正如Benny Neugebauer所提到的,您能够覆盖toString()。 可是,代替覆盖每一个枚举字段的toString,我更喜欢这样的事情: ip
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项关联的字符串。 字符串
您能够简单地使用: get
""+ Modes.mode1
我为您解决的问题! string
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!"); } }
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; } };