在此代碼:如何在重寫java 1.4中的方法時避免「未使用的參數」警告?
public class MyClass {
private Object innerValue;
public Object getInnerValue() {
return this.innerValue;
}
public void setInnerValue(Object innerValue) {
this.innerValue = innerValue;
}
}
public class MyClassReadOnly extends MyClass {
MyClassReadOnly(MyClass cls) {
// Make a field by field copy
super.setInnerValue(cls.getInnerValue());
}
public void setInnerValue(Object innerValue) {
throw new UnsupportedOperationException(
"This is a read-only instance"
);
}
}
編譯器正確地抱怨未使用的參數MyClassReadOnly.setInnerValue()(從未看過)innerValue。
我不想禁用這種警告,因爲它通常非常有用,而且我也不想有任何警告要麼具有較高的信噪比。
,因爲它是Java 1.4中只有我不能使用@SuppressWarnings()結構的另一個問題建議。
我想過插入空這樣的代碼,但它不是很理想:
public void setInnerValue(Object innerValue) {
if (innerValue != null) { /* Do Nothing, but keep the compiler happy */ }
throw new UnsupportedOperationException("This is a read-only instance");
}
@ an0nym0usc0ward:對不起,我一開始沒有提到警告是「innerValue is not read」。你的建議「innerValue = null」也不會讀取參數。 – 2009-04-24 17:11:14