this关键字
含义
1.this的概念:this指代当前对象,持有当前对象的地址
2.如何判断this指代的是哪个对象
官方:this所在的函数,正在被谁调用this就指代谁
民间 :this当前被对象持有,this就指代谁,模板---->复印件
用途
1.区分字段的二义性
Student(String name){
this.name=name;
}
2.this作为返回值(return this;//此时this就是调用方法的这个对象)
public Student add(int a,int b){
System.out.println(a+b);
return this;
}
3.this也可以作为参数传递(pr(this);)
4.this在一个类中,构造方法的第一句调用其他的构造方法
//创建对象,初始化name的值
Student(String name){
this.name=name;
}
//创建对象,初始化name,age的值
Student(String name,int age){
this(name);
this.age=age;
}