在我的三個實例變量中,這些變量都是最終的。但是,當我嘗試在setter方法中執行類似this.inputsRelated = inputsRelated
的操作時,我得到一個可理解的錯誤,它說明最終字段無法分配。但是,設置方法中的方法/我應該如何處理方法public void setInputsRelated(boolean inputsRelated)
和public void setExpected(int expected)
?提前致謝!涉及最終字段的setter方法的問題
public class ExceptionLogicParameters extends RuntimeException {
public final boolean inputsRelated;
public final int expected;
public final int found;
public ExceptionLogicParameters(boolean inputsRelated, int expected, int found)
{
this.inputsRelated = inputsRelated;
this.expected = expected;
this.found = found;
}
@Override
public String toString()
{
return "Get the the hell out of here!";
}
public boolean getInputsRelated()
{
return this.inputsRelated;
}
public int getExpected()
{
return this.expected;
}
public int getFound()
{
return this.found;
}
public void setInputsRelated(boolean inputsRelated)
{
this.inputsRelated = inputsRelated;
}
public void setExpected(int expected)
{
this.expected = expected;
}
}
如果這些字段是最終的,則這些方法不應該存在。必須在聲明或構造函數中設置最終字段。 – axblount 2015-04-02 17:32:44
如果你想重新分配這些字段的值,爲什麼使用final? – 2015-04-02 17:33:18
已解決!教授犯了一個錯誤,最終使實例變量。 – Inferno 2015-04-02 17:49:18