我在一個類中使用下面的代碼來模擬國稅局與僱主文件根據過濾器。我需要重寫equals類,但我不斷收到錯誤,指出在調用對象時無法找到我嘗試使用的方法。如何在Java中的等同重寫方法中投射對象?
@Override
public boolean equals(Object obj) {
if ((this == null)|| (obj == null) || (this.getClass() != obj.getClass()))
return false;
if ((this.sameEmployer((Employer)obj))
&& (this.getEmployeeSSN() == (Employer)obj.getEmployeeSSN())
&& (this.getName() == (Employer)obj.getName())
&& (this.getEmployeeName() == (Employer)obj.getEmployeeName())
&& (this.getEmployeeWages() == (Employer)obj.getEmployeeWages()))
return true;
else
return false;
}
一筆記「這個== NULL」永遠不會發生這樣可以去掉這個 – Loc 2014-10-01 20:53:31
'this.getName()== (Employer)obj.getName()'我猜'getName()'返回一個'String',對嗎?然後使用'this.getName()。equals(((Employer)obj).getName())'代替。 – Tom 2014-10-01 20:57:36