我有兩個類。它們都在構造函數中引用相同的變量。我很困惑,當我使用this.name
時,我不需要更改參數的名稱,Java編譯器知道我指的是主變量?構造函數參數使用這種差異?
在第二個示例中,參數名稱已更改,但未使用this
。 這兩個類別有什麼不同? 他們都認爲「好編程」?
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;
}
}
第二種情況下的'this'是隱含添加的 – Ramanlfc