範例
避免自我比較很簡單,如範例所示,可尋找比較 x == x 的程式碼。 也可以將它擴大,以瞭解簡式 getter 和
this
關鍵字。
public
class
ClassA {
int
x;
public void
methodA() {
if
(x==x){
// ...
}
if
(this.x==x){
// ...
}
if
(getX()==x){
// ...
}
}
}
解決方案
移除 'x==x, this.x==x, getX()==x' 並重新考量程式碼的邏輯