Java this關鍵字詳解


this 關鍵字用來表示當前對象本身,或當前類的一個實例,通過 this 可以調用本對象的所有方法和屬性。例如:
  1. public class Demo{
  2. public int x = 10;
  3. public int y = 15;
  4. public void sum(){
  5. // 通過 this 點取成員變量
  6. int z = this.x + this.y;
  7. System.out.println("x + y = " + z);
  8. }
  9. public static void main(String[] args) {
  10. Demo obj = new Demo();
  11. obj.sum();
  12. }
  13. }
運行結果:
x + y = 25

上面的程序中,obj 是 Demo 類的一個實例,this 與 obj 等價,執行 int z = this.x + this.y;,就相當於執行 int z = obj.x + obj.y;。

注意:this 只有在類實例化后才有意義。

使用this區分同名變量

成員變量與方法內部的變量重名時,希望在方法內部調用成員變量,怎么辦呢?這時候只能使用this,例如:
  1. public class Demo{
  2.     public String name;
  3.     public int age;
  4.   
  5.     public Demo(String name, int age){
  6.         this.name = name;
  7.         this.age = age;
  8.     }
  9.   
  10.     public void say(){
  11.         System.out.println("網站的名字是" + name + ",已經成立了" + age + "年");
  12.     }
  13.   
  14.     public static void main(String[] args) {
  15.         Demo obj = new Demo("微學苑", 3);
  16.         obj.say();
  17.     }
  18. }
運行結果:
網站的名字是微學苑,已經成立了3年

形參的作用域是整個方法體,是局部變量。在Demo()中,形參和成員變量重名,如果不使用this,訪問到的就是局部變量name和age,而不是成員變量。在 say() 中,我們沒有使用 this,因為成員變量的作用域是整個實例,當然也可以加上 this:
  1. public void say(){
  2. System.out.println("網站的名字是" + this.name + ",已經成立了" + this.age + "年");
  3. }
Java 默認將所有成員變量和成員方法與 this 關聯在一起,因此使用 this 在某些情況下是多余的。

作為方法名來初始化對象

也就是相當於調用本類的其它構造方法,它必須作為構造方法的第一句。示例如下:
  1. public class Demo{
  2. public String name;
  3. public int age;
  4. public Demo(){
  5. this("微學苑", 3);
  6. }
  7. public Demo(String name, int age){
  8. this.name = name;
  9. this.age = age;
  10. }
  11. public void say(){
  12. System.out.println("網站的名字是" + name + ",已經成立了" + age + "年");
  13. }
  14. public static void main(String[] args) {
  15. Demo obj = new Demo();
  16. obj.say();
  17. }
  18. }
運行結果:
網站的名字是微學苑,已經成立了3年

值得注意的是:
  • 在構造方法中調用另一個構造方法,調用動作必須置於最起始的位置。
  • 不能在構造方法以外的任何方法內調用構造方法。
  • 在一個構造方法內只能調用一個構造方法。

上述代碼涉及到方法重載,即Java允許出現多個同名方法,只要參數不同就可以。后續章節會講解。

作為參數傳遞

需要在某些完全分離的類中調用一個方法,並將當前對象的一個引用作為參數傳遞時。例如:
  1. public class Demo{
  2. public static void main(String[] args){
  3. B b = new B(new A());
  4. }
  5. }
  6. class A{
  7. public A(){
  8. new B(this).print(); // 匿名對象
  9. }
  10. public void print(){
  11. System.out.println("Hello from A!");
  12. }
  13. }
  14. class B{
  15. A a;
  16. public B(A a){
  17. this.a = a;
  18. }
  19. public void print() {
  20. a.print();
  21. System.out.println("Hello from B!");
  22. }
  23. }
運行結果:
Hello from A!
Hello from B!

匿名對象就是沒有名字的對象。如果對象只使用一次,就可以作為匿名對象,代碼中 new B(this).print(); 等價於 ( new B(this) ).print();,先通過 new B(this) 創建一個沒有名字的對象,再調用它的方法。
Java輔導班
<上一節下一節>
被頂起來的評論
  • .源代碼
    .源代碼

    package thisfangfa;
    public class This {
    public static void main(String[] args){
    B b=new B(new A());//這里創建B類的對象b時,對B類的構造函數傳入了
    //A類的對象的引用(指針),
    }
    }
    class A{
    public A()
    {
    System.out.println www.uuweb.cn ("step 1 ");
    new B(this).print();//程序第一步會執行A類的構造方法A(),因為創建B類
    //的對象b時,對B類的構造函數傳入了A類的對象的引用(指針),所以程序先執行
    //new A() ,執行new A()的時候就運行了A類的構造方法。A類的構造方法里面
    //創建了B類的匿名對象,並且引用了B類匿名對象的print();方法。
    }
    public void print()
    {
    System.out.println("step 3");//由於第二步a.print();執行了A類的
    //print()方法。所以程序第三步會運行到這里。
    System.out.println("hello from A");//然后執行到這句
    //代碼,所以打印輸出了hello from A
    }
    }
    class B{
    A a;//這里先聲明一個A類的對象a,這時還沒有對a實例化,只是聲明。
    public B(A a){//執行new A()完畢后,開始執行執行new B()了,這時會執行B類的
    //構造函數B(),
    this.a=a;//B b=new B(new A());//這里創建B類的對象b時,傳入的A類對象
    //的引用(指針),賦予了之前聲明的A類的對象a,這時a存儲着創建B類的對象b時,傳
    //入的A類對象的引用(指針)。
    }
    public void print()
    { System.out.println("step 2 ");//由於引用了B類匿名對象的print()方法。
    //所以程序第二步會執行到這個方法里面。
    a.print();//因為a存儲着創建B類的對象b時,傳入的A類對象的引用(指針)。
    //所以這里執行a對象的方法print(),就是執行的A這個類里面的
    //print()方法。所以程序第二步會運行到這里。這里執行了A類的print()方法。
    //
    //打印輸出hello from A完畢后,a.print();執行完畢,
    System.out.println("step 4 ");//所以開始執行這句
    System.out.println("hello from B");//和執行這句
    //整個程序結束在這里。
    }
    }

    在Eclipse下程序輸出結果是這樣的:

    step 1 
    step 2 
    step 3
    hello from A
    step 4 
    hello from B


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM