Using the this Keyword
Within an instance method or a constructor, this
is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this
.
在实例方法或构造方法里面,this
是对当前对象的引用 —— 即正在调用方法或构造方法的对象。可以使用 this
从实例方法或构造方法中引用当前对象的任何成员。
Using this
with a Field 与字段一起使用
The most common reason for using the this
keyword is because a field is shadowed by a method or constructor parameter.
使用 this
关键词最常见的原因是字段被方法或构造方法的参数所遮蔽。
For example, the Point
class was written like this
例如,Point
类是这样编写的
public class Point {
public int x = 0;
public int y = 0;
//constructor
public Point(int a, int b) {