查缺補漏之——this關鍵字

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")	;
	}

結果:

image

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章