this關鍵字的作用:
調用類中的屬性
調用類中的方法或構造方法
表示當前對象
public class Test { public static void main(String[] args) { Person p1=new Person(); } } class Person{ private String name; private char sex; public Person(){ this("張爺爺"); } public Person(String name){ this(name,'男'); } public Person(String name,char sex){ this.name=name; //表示當前對象屬性 this.sex=sex; //表示當前對象屬性 this.fight(); //調用本類中的方法 } public void fight(){ System.out.println("呔!"+this.name+"在此,誰敢跟俺一戰!"); } }