this關鍵字
this.屬性
訪問類中的屬性
this.方法名
訪問類中的方法
this()
調用類的構造方法——注意該條語句包含在構造方法中的第一條語句
代碼演示
public class People {
private Integer age ;
private String name ;
public People(String name){
this.name = name ;
}
public People(Integer age , String name){
this("this(咋回事?)");
this.age = age ;
System.out.println("this(咋回事?)調用了構造方法People(String name)");
System.out.println(this.getName());
}
省略getter和setter
}
測試代碼
public void test3() {
People p = new People(21,"mei") ;
}
結果: