java return this 的笔记
rerurn this用来返回当前对象的引用
比如:Person person;这里的person就是一个Person的引用
Person person = new Person();–>person指向Person对象的地址
public class testThis {
public static void main(String[] args) {
testThis t= new testThis();
t.A().B();
}
testThis A() {
System.out.println("A");
return this;
}
void B() {
System.out.println("B");
}
}
执行结果: A B
t.A() 后返回了当前对象的引用,等于说B方法其实也是被 t 这个引用调用的!