Answers:
你不能 我认为您在这里有四个选择。四个都提供了解决方案,但方法略有不同。
选项一: 在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;
}
}
选项三: 使用静态的final代替枚举:
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 interface Modes {
String MODE_1 = "Fancy Mode 1";
String MODE_2 = "Fancy Mode 2";
String MODE_3 = "Fancy Mode 3";
}
每个枚举都具有name()和valueOf(String)方法。前者返回枚举的字符串名称,后者给出名称为字符串的枚举值。这是您想要的吗?
String name = Modes.mode1.name();
Modes mode = Modes.valueOf(name);
Enum本身也有一个静态valueOf(Class,String),因此您也可以使用
Modes mode = Enum.valueOf(Modes.class, name);
some-really-long-string
。
您可以覆盖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
}
正如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项关联的字符串。
mode1.name()
或String.valueOf(mode1)
。恐怕没有比这更好的了
您可以使用,Mode.mode1.name()
但是您通常不需要这样做。
Mode mode =
System.out.println("The mode is "+mode);
name()
并toString()
可以被覆盖,但希望这将通过阅读代码的明确enum
,如果发生这种情况。
name()
是final
。谢谢你纠正我。:)
据我所知,唯一获得名称的方法是
Mode.mode1.name();
但是,如果您确实需要这种方式,则可以执行以下操作:
public enum Modes {
mode1 ("Mode1"),
mode2 ("Mode2"),
mode3 ("Mode3");
private String name;
private Modes(String s) {
name = s;
}
}
Mode.mode1
仍然不是type String
。
您可以简单地使用:
""+ Modes.mode1
"" + Modes.mode1
。我确定了答案
''.join()
我为您解决的问题!
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;
}
};
public enum Environment
{
PROD("https://prod.domain.com:1088/"),
SIT("https://sit.domain.com:2019/"),
CIT("https://cit.domain.com:8080/"),
DEV("https://dev.domain.com:21323/");
private String url;
Environment(String envUrl) {
this.url = envUrl;
}
public String getUrl() {
return url;
}
}
String prodUrl = Environment.PROD.getUrl();
它将打印:
https://prod.domain.com:1088/
枚举字符串常量的这种设计在大多数情况下都有效。
经过多次尝试,我提出了这种解决方案
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";
}
}
您可以尝试以下方法:
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 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());
.name()
查看:stackoverflow.com/a/6667365/887836