java Java枚举和if语句,有没有更好的方法来做到这一点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5983150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java enum and if statement, is there a better way to do this?
提问by Thang Pham
I have an java Enum
class, and at runtime I will read in a value from command line, and I want to correspond this value to a value in my Enum
class. Shipper
is my Enum Class. Is there a better way do this this, instead of if
and else if
below? This look ugly.
我有一个 javaEnum
类,在运行时我将从命令行读取一个值,我想将此值与我的Enum
类中的值对应起来。Shipper
是我的枚举类。有没有更好的办法做这个这个,而不是if
和else if
下面?这个样子难看。
private List<Shipper> shipperName = new ArrayList<Shipper>();
...
public void init(String s){
if(s.equals("a")){
shipperName.add(Shipper.A);
}else if(s.equals("b")){
shipperName.add(Shipper.B);
}else if(s.equals("c")){
shipperName.add(Shipper.C);
}else if(s.equals("d")){
shipperName.add(Shipper.D);
}else if(s.equals("e")){
shipperName.add(Shipper.E);
}else{
System.out.println("Error");
}
}
Here is my Shipper.class
这是我的 Shipper.class
public enum Shipper
{
A("a"),
B("b"),
C("c"),
D("e"),
F("f")
;
private String directoryName;
private Shipper(String directoryName)
{
this.directoryName = directoryName;
}
public String getDirectoryName()
{
return directoryName;
}
}
回答by planetjones
Yes add the string into the enum's constructor (specify a constructor in the enum which takes a String) and create the enums with a text value of a, b , c. Etc. Then implement a static factory method on the enum which takes a string and returns the enum instance. I call this method textValueOf. The existing valueOf method in enum cannot be used for this. Something like this:
是的,将字符串添加到枚举的构造函数中(在枚举中指定一个接受字符串的构造函数)并创建文本值为 a、b、c 的枚举。等等。然后在枚举上实现一个静态工厂方法,它接受一个字符串并返回枚举实例。我将此方法称为 textValueOf。枚举中现有的 valueOf 方法不能用于此。像这样的东西:
public enum EnumWithValueOf {
VALUE_1("A"), VALUE_2("B"), VALUE_3("C");
private String textValue;
EnumWithValueOf(String textValue) {
this.textValue = textValue;
}
public static EnumWithValueOf textValueOf(String textValue){
for(EnumWithValueOf value : values()) {
if(value.textValue.equals(textValue)) {
return value;
}
}
throw new IllegalArgumentException("No EnumWithValueOf
for value: " + textValue);
}
}
This is case insensitive and the text value can be different to the enum name - perfect if your database codes are esoteric or non descriptive, but you want better names in the Java code. Client code then do:
这是不区分大小写的,文本值可以与枚举名称不同 - 如果您的数据库代码是深奥的或非描述性的,那么完美,但您希望在 Java 代码中使用更好的名称。客户端代码然后执行:
EnumWithValueOf enumRepresentation = EnumWithValueOf.textValueOf("a");
回答by JustinKSU
shipperName.add(Shipper.valueOf(s.toUpperCase()));
If the name doesn't always match the enumeration you can do something like this
如果名称并不总是与枚举匹配,您可以执行以下操作
public static Shipper getShipper(String directoryName) {
for(Shipper shipper : Shipper.values()) {
if(shipper.getDirectoryName().equals(directoryName)) {
return shipper;
}
}
return null; //Or thrown exception
}
回答by Darkzaelus
You can get an Enum from the valueOf() method:
您可以从 valueOf() 方法获取 Enum:
public void init(String s) {
try {
Shipper shipper = Shipper.valueOf(s.toUpperCase());
shipperName.add(shipper);
} catch(IllegalArgumentException e) {
e.printStackTrace();
}
}
EDIT:
编辑:
If it is not just a simple a -> A case, then it might be an idea to create a HashMap with key=>value pairs:
如果它不仅仅是一个简单的 a -> A 案例,那么创建一个具有 key=>value 对的 HashMap 可能是一个想法:
private HashMap<String, Shipper> map;
public void init(String s) {
if(map == null) {
map = new HashMap<String, Shipper>();
map.put("a", Shipper.A);
... //b => B code etc
}
if(map.containsKey(s)) {
shipperName.add(map.get(s));
} else {
System.out.println("Error");
}
}
I hope this helps!
我希望这有帮助!