Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我刚开始使用greenfoot,我正在研究一些例子,发现人们经常使用“this”,它到底是什么意思,我不太明白,例如:
//set the animal's age public void setAge(int age) { this.age = age; }
关键字
this
指的是封闭类的一个实例。在您的示例中,“this”将引用包含该关键字的类。
您的问题可能已经在这里得到解答:
Java中的“this”是什么意思?
this指的是对象的当前实例。因此,在您的示例中:
public void setAge(int age) { this.age = age; }
this在setter 中使用 ofsetAge意味着我们在 中引用实例变量age,this.age并将其设置为参数的值age。这意味着this.age并age指代不同的变量。
setAge
age
this.age