如果在方法內部調用同一個類的另一個方法,不需使用this,直接調用即可。當前方法中的this引用會自動應用于同一個類中 的其他方法
public class Person{ public void eat(){...} public void action(){ eat(); // eat(); --> this.eat(); 一般省略this關鍵字 }} 在action內部,可以寫成this.eat(); 但是編譯器可以自動添加,所以沒必要自己添加。 一般只有在需要明確指出對當前對象的引用時才需要使用this關鍵字 2.區(qū)別全局變量和局部變量,引用成員變量 當出現了局部變量和全局變量重名的時候,需要對全局變量和局部變量進行區(qū)分,使用this進行區(qū)分 this指的是當前調用者 this.變量名 指的是成員變量 this指的是當前調用者,當前調用該方法的對象 public class Animal{ public static void main(String[] args){ Person p1 = new Person("zs"); Person p2 = new Person("ww"); System.out.PRintln("p1.name="+p1.name); //zs p1.eat(); //zs在吃東西 p2.eat(); //ww在吃東西 } } class Person{ public String name; public int age; public char gender; public Person(){ } public Person(String name){ this.name = name; } //當出現了局部變量和全局變量重名的時候,需要對全局變量和局部變量進行區(qū)分,使用this進行區(qū)分 public void eat(){ String name = "ls"; System.out.println(this.name+"在吃東西"); } } 1).在Animal中調用Person的構造方法時,帶參數的構造方法public Person(String name)中參數name與成員變量name同名,如果不使用this調用成員變量,則編譯器會根據就近原則,認為name=name兩個name都是參數列表的name而不是成員變量name,所以在行5中,輸出結果為p1.name=null。所以要使用this明確指出調用當前對象的成員變量name:this.name=name,此時行5結果為:p1.name=zs 2)在Animal中調用Person的eat()時,當eat()中輸出直接調用name時,會就近原則調用eat()中的局部變量name,所以p1.eat()結果為:ls在吃東西;當在eat()中輸出用this調用name,此時調用的時成員變量name,所以p1.eat()結果為:zs在吃東西,p2.eat()結果為:ww在吃東西 3.在構造器中調用其他構造器 this(); 調用本類中空的構造方法 this(參數列表); 表示對符合此參數列表的某個構造方法的明確調用 通過this調用其他構造方法,必須讓該語句方法在構造方法的第一行,并且該構造方法只能有一個this調用其他構造方法的語句
1 public class Person{2 public void eat(){...}3 public void action(){4 eat(); // eat(); --> this.eat(); 一般省略this關鍵字5 }6 }public class Animal{ public static void main(String[] args){ Person p = new Person("zs",12,'男'); //兩個帶參數的構造方法都調用了 } }class Person{ public String name; public int age; public char gender; public Person(){ System.out.println("初始無參構造方法"); } public Person(String name){ System.out.println("初始name構造方法"); this.name = name; } public Person(String name,int age){ this(name); this.age = age; System.out.println("初始name和age構造方法"); } /* //錯誤的寫法:對this的調用必須是構造器中的第一個語句,同一個構造方法中不能出現兩個this對其他構造器進行調用 public Person(String name,int age){ this(); this(name); this.age = age; System.out.println("初始name和age構造方法"); } */ public Person(String name,int age,char gender){ this(name,age); this.gender = gender; }} 在上段代碼中,Animal中創(chuàng)建Person對象p,調用構造方法public Person(String name,int age,char gender),此方法通過this(name,age);調用public Person(String name,int age),此構造方法又調用public Person(String name)構造方法 結果:System.out.println("初始name和age構造方法"); System.out.println("初始name構造方法"); 好處:避免重復代碼
public class Flower{ int petalCount = 0; //花瓣數量 String s = "initial value"; Flower(int petals){ petalCount = petals; System.out.println("初始化petals的構造方法,petalCount="+petalCount); } Flower(String ss){ System.out.println("初始化ss的構造方法,ss="+s); } Flower(String s,int petals){ this(petals); //this(s); //同一個構造方法不能this兩個 this.s = s; //this.s指的是成員變量s System.out.println("String & int 參數列表"); } Flower(){ this("hi",47); //this(s); //同一個構造方法不能this兩個 this.s = s; //this.s指的是成員變量s System.out.println("無參數構造方法"); } public void printPetalCount(){ //this(11); //不能在其他方法中使用this()調用構造器,只能在構造器中使用 System.out.println("petalCount="+petalCount+" s="s); } public static void main(String[] args){ Flower flower = new Flower(); x.printPetalCount(); } /* 輸出結果: 初始化petals的構造方法,petalCount=47 String & int 參數列表 無參數構造方法 petalCount=47 s=hi */ } 構造器Flower(String s,int petals)表明:可以用this調用一個構造器,但卻不能調用兩個,并且必須將構造器調用放置于第一行最起始處,否則會報錯 在構造器Flower(String s,int petals)中,參數s的名稱和數據成員s的名字相同,所以會產生歧義,使用this來區(qū)分
printPetalCount方法表明,除構造器外,編譯器禁止在其他任何方法中調用構造器
新聞熱點
疑難解答