一、this的指向
(1)如果出现在构造器中:表示正在创建的对象
(2)如果出现在成员方法中:表示正在调用这个方法的对象
二、this的用法
(1)this.属性
当局部变量与成员变量同名时,那么可以在成员变量的而前面加“this.”用于区别
(2)this.方法
调用当前对象的成员方法,完全可以省略“this.”
(3)this()或this(实参列表)
this()表示调用本类的无参构造
this(实参列表)表示调用本类的有参构造
this()或this(实参列表)要么没有,要么必须出现在构造器的首行
三、示例
public class Student {
private String name;
private int score;
public Student() {
}
public Student(String name) {
this.name = name;
}
public Student(String name, int score) {
this(name);
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setScore(int score) {
this.score = score;
}
public int getScore() {
return score;
}
}