私は2つのクラスを持っています。どちらもコンストラクタ内の同じ変数を参照します。私が混乱している、私が使用したときthis.name
私はパラメータの名前を変更する必要はありません、Javaコンパイラは私が主な変数を参照していることを知っている?この相違点を使用するコンストラクタパラメータ?
第2のクラスの例では、パラメータ名は変更されましたが、this
は使用されていません。 これら2つのクラスの違いは何ですか? 彼らはどちらも「良いプログラミング」と見なされていますか?
class Account {
private string name;
private int amount;
Account(String name, int amount) {
this.name = name;
this.amount = amount;
}
}
class Account {
private string name;
private int amount;
Account(String n, int a) {
name = n;
amount = a;
}
}
第2の場合の 'this'は暗黙的に追加されます – Ramanlfc