问题描述
当我在eclipse中将jdk1.6升级为jdk1.7时,以下错误已经开始抛出编译模式。
When I have upgrade java version from jdk1.6 to jdk1.7 in eclipse, The below error has started to throw in compile mode only.
错误是:
public enum TestEnum {
ABC;
public static TestEnum valueOf(Class<TestEnum> enumType, String value){//This error throw at this line.
return null;
};
}
当我将编译级别从1.7更改为1.6时,错误走了。
我不明白为什么这不能与编译级别1.7工作?有没有其他的变化需要编译1.7级?
When I have changed compile level from 1.7 to 1.6, error has gone.I do't understand why is this not working with compile level 1.7? is there any other changes need to compile level 1.7?
编辑:
它返回具有指定名称的指定枚举类型的枚举常量。该名称必须与用于声明此类型的枚举常量的标识符完全一致。但我没有这样的情况,名字是不同的。有100多个枚举类具有相同的实现。对我而言,改变这种广泛变化是不容易的。
It returns the enum constant of the specified enum type with the specified name. The name must match exactly an identifier used to declare an enum constant in this type. but I have not such case, the name is different.There are above 100 enum classes which has same implementation. it is not easy for me to change such product wide change.
public enum TestEnum {
ABC;
public String toString() {
switch (this) {
case ABC:
return "Abc";
}
return "";
}
/* public static TestEnum valueOf(Class<TestEnum> enumType, String value){
if(value.equalsIgnoreCase("XYZ")){
return ABC;
}
return null;
};*/
public static void main(String[] args) {
TestEnum t=TestEnum.valueOf(TestEnum.class,"Abc");
System.out.println(t);
}
}
推荐答案
问题是 static< T extends Enum< T>> T Enum.valueOf(类< T> enumType,String name)
与您的不相同public static TestEnum valueOf(Class&Test; TestEnum> enumType,String value)
,但在类型擦除后看起来一样(两者基本上都变成静态Object valueOf(Class enumType,String value)。
The problem is that static <T extends Enum<T>> T Enum.valueOf(Class<T> enumType, String name)
is not the same as your public static TestEnum valueOf(Class<TestEnum> enumType, String value)
, but look the same after type erasure (both essentially become static Object valueOf(Class enumType, String value).
由于这可能导致不直观的行为, Java创建者决定这应该是一个错误;相应的检查是在Java 1.7中实现的,如果您的 valueOf
方法的行为(或应该是行为)像 Enum.valueOf
,您可以简单地删除它,否则重命名可以解决问题。
Since this can lead to unintuitive behavior, the Java creators decided that this should be an error; the corresponding check was implemented in Java 1.7. If your valueOf
method behaves (or should behave) like Enum.valueOf
, you can simply remove it. Otherwise, renaming it could solve the problem.
这篇关于(错误:类型为Enum< E>的ValueOf(Class< T>,String)但不隐藏它)将版本从jdk1.6升级到jdk1.7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!