public class Student {
public String id;
public String name;
public Set course;
public Student(String id, String name) {
this.id = id; // 必須 this,參數同名
this.name = name; // 必須 this,參數同名
this.course = new HashSet(); // 可省略 this
}
}
this.member 區分,否則賦值給參數自身。this 可省略,編譯器自動解析為成員變數。this.course = new HashSet() 中 course 無同名參數,故 this 可寫可不寫。this. 或全部省略,避免混淆。Settings → Code Style → Java → Use 'this' qualifier for field access 統一格式化。id_、name_ 或 pId,避免同名,則可省略 this。「最後一行可以不加,倒數第二行必須加,因為參數name和成員name同名,必須用this加以區分。」