Convert String to Enum?
Possible Duplicate:
Java - Convert String to enum
I have a method that uses an enum:
mymethod(AnotherClass.MyEnum.PassedEnum);
And I want to nest this within a class that receives a String which becomes MyEnum:
public static void method(String toPass){
mymethod(AnotherClass.toPass.PassedEnum);
}
The passed variable has to be a String but I need to convert it to a Enum to pass to AnotherClass?
TIA
使用AnotherClass.MyEnum.valueOf(toPass)
Do you mean like ...
MyEnum e = MyEnum.valueOf(text);
or
MyEnum e = Enum.valueOf(MyEnum.class, text);
我认为静态工厂方法Enum.valueOf()
做你想做的。
上一篇: Java将一个字符串转换为枚举列表
下一篇: 将字符串转换为枚举?