plutotw
井底蛙
   
發文: 624
積分: 3
|
於 2004-11-09 22:18
     
1 2 3 4 5 6
| class T {
private String text ;
public setText( String text ) {
this.text = text ;
}
}
|
上面的寫法習見於一般碼(甚或 IDE 產生出來的碼) 但如果程式碼變成了
1 2 3
| public setText( String text ) {
this.text = text.toUpperCase() ;
}
|
看來無傷 ,但難保有一天程式碼不會...
1 2 3 4 5 6 7
| public setText( String text ) {
this.text = text.toUpperCase() ;
................
................
................
foo( text ) ;
}
|
這樣就形成災難 . 剛剛才追到別人寫的這樣的一個 bug 所以建議, method 進來的變數 ,最好不要與 class 內的 attrib 同名,進而去設定 ,而用到 this. , 因為你不曉得什麼時候會再一直擴增.. 而不小心用錯了變數
  |