关于this的几个用法
隐式参数
this可以作为隐式参数的代指
public add(int n,int s){
this.name=n;
this.salary=s;
}
比如上面的用法。
代指同类下其他方法
this在构造器的第一行如果使用形如this(…)的方式,表示的是这个构造器将调用同类下面的另一个符合要求的构造器
public Employee(double s){
this("Employee #"+nextId,s);
nextId++;
}
上面在调用new Employee(6000)时,Employee(double)构造器会先调用Employee(String,double)构造器。
采用这种方式使用this关键字非常有用,这样对公共的构造器代码只需要编写一次就可以了。
https://blog.csdn.net/shiyansama/article/details/109394552