我使用的Java 6和我的應用程序是巨大的,它在Java開發的6 現在我們要升級Java版本爲7的valueOf()枚舉法是不是在java中工作7
但是,當我試過它到java 7它會給enum類編譯錯誤。 在枚舉類中,我定義了valueOf()方法,所以在Java 7中它給編譯錯誤。
代碼
public enum TestEnum {
TESTONE,TESTTWO, NONE;
public String toString() {
switch(this) {
case TESTONE:
return "Test one";
case TESTTWO:
return "Test two";
case NONE:
return "None";
}
return null;
};
public static TestEnum valueOf(Class<TestEnum> enumType, String value){
if(value.equalsIgnoreCase(TESTONE.toString()))
return TestEnum.TESTONE;
else if(value.equalsIgnoreCase(TESTTWO.toString()))
return TestEnum.TESTTWO;
else if(value.equalsIgnoreCase(NONE.toString()))
return TestEnum.NONE;
else
return null;
}
}
錯誤
Name clash: The method valueOf(Class<TestEnum>, String) of type TestEnum has the
same erasure as valueOf(Class<T>, String) of type Enum<E> but does not hide it TestEnum.java
更新(解決)
我改變的valueOf()方法和我的文件編譯。
public static TestEnum valueOf(TestEnum enumType, String value){
if(value.equalsIgnoreCase(TESTONE.toString()))
return TestEnum.TESTONE;
else if(value.equalsIgnoreCase(TESTTWO.toString()))
return TestEnum.TESTTWO;
else if(value.equalsIgnoreCase(NONE.toString()))
return TestEnum.NONE;
else
return null;
}
一方面,在'類'參數未使用;對於另一個,它是多餘的,因爲該類型的唯一類是'TestEnum.class'。 –
這段代碼在TestEnum中得到了'error:name clash:valueOf(,String)類,並且中的valueOf(Class ,String)具有相同的擦除,但在使用'-source 1.6'編譯時不隱藏另一個'。 –
EJP
@AndyTurner謝謝我刪除類<>及其工作 –