谁调用,表明谁函数
this.属性名this
//例如类中的Set方法 public void setAge(int age){ this.age=age; //this.age表明当前调用setAge()方法的对象所对应的Age值 }
this(形参1,形参2......形参n),其中形参是根据构造方法来安排的code
public class Person{ private int age; public Person(){ } public Person(int age){ this();//必定要放在首行 //用this(形参,形参2.....形参n)时, //要留一个构造函数做为出口,默认留着无参构造(本身写出来)做为出口 this.age=age; } }
this //以String类中的toString方法为例 public String toString() { return this;//返回的是调用toString方法的字符串自己 } //常规来说,若是是打印变量默认调用的都是toString方法,默认其实打印为地址, //但String类中重写了这个方法,因此返回值就是调用的字符串