java this 关键字3种使用场景

java 中 this 关键字的3中使用场景

1、参数赋值时解决歧义
String name = "bob" ;
public void setName(name){
this.name = name
}

2、方法中返回自身引用
public class Person{
public Person getPerson(){
return this;
}
}

3、构造器中调用同类的另一个构造器
this("hello")
只能掉用一次,并且在第一句。

参考资料:《java编程思想》5.4 节 this关键字

猜你喜欢

转载自blog.51cto.com/19941018/2430651