查缺补漏之——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

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