this與super關鍵字總結


 Ⅰ.this

  用類名定義一個變量的時候,定義的應該只是一個引用,外面可以通過這個引用來訪問這個類里面的屬性和方法。類里面也有一個引用來訪問自己的屬性和方法,這個引用就是 this 對象,它可以在類里面來引用這個類的屬性和方法。

  每當一個對象創建后,Java虛擬機會給這個對象分配一個引用自身的指針,這個指針的名字就是 this。因此,this只能在類中的非靜態方法中使用,靜態方法和靜態的代碼塊中絕對不能出現this的用法。

  this主要要三種用法:

  1.   在類的方法定義中使用的this關鍵字代表調用該方法對象的引用。(this關鍵字只能在方法內部使用;只有當需要明確指出當前對象的引用時,才使用this關鍵字)
  2.   表示用類的成員變量,而非函數參數,注意在函數參數和成員變量同名是進行區分!其實這是第一種用法的特例,比較常用,所以拿出來強調一下。
  3.   將當前的對象傳遞給其他方法。
  4.   用於在構造方法中引用滿足指定參數類型的構造器(其實也就是構造方法)。但是這里必須非常注意:只能引用一個構造方法且必須位於開始!

示例程序:

eg1

 1 public class ThisDemo {  
 2     int number = 0;
 3     ThisDemo increment(){
 4          number++;
 5          return this;
 6     }  
 7   private void print(){
 8          System.out.println("number="+number);
 9     }
10     public static void main(String[] args) {
11         ThisDemo tt=new ThisDemo();
12          tt.increment().increment().increment().print();
13     }
14 }
15 /*Output:
16 i = 3
17 *///:~

可以看到,通過this關鍵字可以很容易在一條語句里面對同一個對象執行多次操作。

 

eg2 將當前的對象傳遞給其他方法

 1 //: initialization/PassingThis.java
 2 
 3 class Person {
 4   public void eat(Apple apple) {
 5     Apple peeled = apple.getPeeled();
 6     System.out.println("Yummy");
 7   }
 8 }
 9 
10 class Peeler {
11   static Apple peel(Apple apple) {
12     // ... remove peel
13     return apple; // Peeled
14   }
15 }
16 
17 class Apple {
18   Apple getPeeled() { return Peeler.peel(this); }
19 }
20 
21 public class PassingThis {
22   public static void main(String[] args) {
23     new Person().eat(new Apple());
24   }
25 } /* Output:
26 Yummy
27 *///:~

Apple需要調用Peeler.peel()方法,它是一個外部的工具方法,將執行由於某種原因而必須放在Apple外部的操作。為了將其自身傳遞給外部方法,Apple必須使用this關鍵字。

 

eg3 在構造方法中引用滿足指定參數類型的構造器

 1 //: initialization/Flower.java
 2 // Calling constructors with "this"
 3 import static net.mindview.util.Print.*;
 4 
 5 public class Flower {
 6   int petalCount = 0;
 7   String s = "initial value";
 8   Flower(int petals) {
 9     petalCount = petals;
10     print("Constructor w/ int arg only, petalCount= "
11       + petalCount);
12   }
13   Flower(String ss) {
14     print("Constructor w/ String arg only, s = " + ss);
15     s = ss;
16   }
17   Flower(String s, int petals) {
18     this(petals);
19 //!    this(s); // Can't call two!
20     this.s = s; // Another use of "this" 參數s和數據成員s的名字相同,用this.s來避免歧義。
21     print("String & int args");
22   }
23   Flower() {
24     this("hi", 47);
25     print("default constructor (no args)");
26   }
27   void printPetalCount() {
28 //! this(11); // Not inside non-constructor!
29     print("petalCount = " + petalCount + " s = "+ s);
30   }
31   public static void main(String[] args) {
32     Flower x = new Flower();
33     x.printPetalCount();
34   }
35 } /* Output:
36 Constructor w/ int arg only, petalCount= 47
37 String & int args
38 default constructor (no args)
39 petalCount = 47 s = hi
40 *///:~

構造器Flower(String s, int petals)表明:盡管可以用this調用一個構造器,但卻不能調用兩個。此外,必須將構造器調用置於最起始位置處,否則編譯器會報錯。

printPetalCount()方法表明,除構造器之外,編譯器禁止在其他任何方法中調用構造器。

 Ⅱ.super

  super的使用和this基本相同,現在寫下兩者之間的比較:

  1.super()從子類中調用父類的構造方法,this()在同一類內調用其它方法。

  2.this和super不能同時出現在一個構造函數里面。

  3.super()和this()均需放在構造方法內第一行。

  4.this()和super()都指的是對象,所以,均不可以在static環境中使用。

 

另外1.在構造器中this或super必須放在第一行 

  必須在構造器的第一行放置super或者this構造器,否則編譯器會自動地放一個空參數的super構造器的,其他的構造器也可以調用super或者this,調用成一個遞歸構造鏈,最后的結果是父類的構造器(可能有多級父類構造器)始終在子類的構造器之前執行,遞歸的調用父類構造器。無法執行當前的類的構造器。也就不能實例化任何對象,這個類就成為一個無為類。
   從另外一面說,子類是從父類繼承而來,繼承了父類的屬性和方法,如果在子類中先不完成父類的成員的初始化,則子類無法使用,應為在java中不允許調用沒初始化的成員。在構造器中是順序執行的,也就是說必須在第一行進行父類的初始化。而super能直接完成這個功能。This()通過調用本類中的其他構造器也能完成這個功能。因此,this()或者super()必須放在第一行。
 

另外2(static的含義)

  了解this關鍵字之后,就能更全面的理解static方法的含義。static方法沒有this的方法。在static方法的內部不能調用非靜態方法,但反過來可以。可以在沒有創建任何對象的前提下,僅僅通過類本身來調用static方法。但這實際上正是static方法的主要用途。Java中禁止使用全局方法,但通過在類中置入static方法就可以訪問其他static方法和static域。


免責聲明!

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



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