[英]Java's Enum.valueOf() problem
I'm trying to derive an enum value from a String, like:我正在尝试从字符串中派生枚举值,例如:
Level level = Enum.valueOf(Level.class, "WARNING");
But all I get is compiler warnings like:但我得到的只是编译器警告,例如:
Test.java:8: <T>valueOf(java.lang.Class<T>,java.lang.String) in java.lang.Enum cannot be applied to (java.lang.Class<java.util.logging.Level>,java.lang.String)
I know in JDK versions prior to 1.5 this:我知道在 1.5 之前的 JDK 版本中:
Level level = Level.valueOf("WARNING");
would work, but I'm using JDK 1.6.会工作,但我使用的是JDK 1.6。 Could anyone provide a working example for this kind of problem?任何人都可以为此类问题提供一个工作示例吗?
Thanks.谢谢。
java.util.logging.Level
simply isn't an enum. java.util.logging.Level
根本不是枚举。 Did you actually mean that Level
class, or a different one?您实际上是指class Level
还是其他级别?
The second snippet you posted wouldn't work either, but if you're really talking about the normal Level
class, you can use:您发布的第二个片段也不起作用,但如果您真的在谈论普通Level
class,您可以使用:
Level level = Level.parse("WARNING");
Level.WARNING
, as well as Level.INFO
and other Loggin levels are no ENUM's. Level.WARNING
以及Level.INFO
和其他 Loggin 级别都不是 ENUM。
They're static final
variables in the Level.java
class.它们是 Level.java class 中的Level.java
static final
变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.