簡體   English   中英

Java中“ this”和“ super”關鍵字之間的區別

[英]Difference between “this” and“super” keywords in Java

關鍵字thissuper和有什么不一樣?

兩者都用來訪問類的構造函數吧? 你們任何人都可以解釋嗎?

讓我們考慮這種情況

class Animal {
  void eat() {
    System.out.println("animal : eat");
  }
}

class Dog extends Animal {
  void eat() {
    System.out.println("dog : eat");
  }
  void anotherEat() {
    super.eat();
  }
}

public class Test {
  public static void main(String[] args) {
    Animal a = new Animal();
    a.eat();
    Dog d = new Dog();
    d.eat();
    d.anotherEat();
  }
}

輸出將是

animal : eat
dog : eat
animal : eat

第三行正在打印“ animal:eat”,因為我們正在調用super.eat() 如果我們調用this.eat() ,它將被打印為“ dog:eat”。

super用於同時訪問的基類的方法this用於當前類的訪問方法。

擴展這個概念,如果您編寫super() ,它是指基類的構造函數;如果您編寫this() ,則是指您在其中編寫此代碼的類的構造函數。

this是對類型為當前類的對象的引用,而super是對類型為其父類的對象的引用。

在構造函數中, this()調用當前類中定義的構造函數。 super()調用在父類中定義的構造函數。 構造函數可以在任何父類中定義,但是它將引用最接近當前類的一個重寫。 以這種方式對其他構造函數的調用只能作為構造函數的第一行完成。

調用方法的工作方式相同。 調用this.method()調用當前類中定義的方法,其中super.method()會調用與父類中定義的方法相同的方法。

從您的問題中,我認為您確實是在詢問在構造函數鏈接中使用thissuper問題。 例如

public class A extends B {
    public A(...) {
        this(...);
        ...
    }
}

public class A extends B {
    public A(...) {
        super(...);
        ...
    }
}

區別很簡單:

  • this表單鏈接到當前類中的構造函數。 即在A班。

  • super表單鏈接到直接超類中的構造函數。 即在B類中。

this是指當前類的引用。
super引用當前類的級(稱為super關鍵字)。

通過這樣做this ,它可以讓你訪問方法/當前類的屬性(包括其自己的私有方法/屬性)。

super允許您訪問父(基)類的公共/受保護的方法/屬性。 您看不到父級的私有方法/屬性。

super()和this()

  • super()-調用父類的構造函數。
  • this()-調用同一類的構造函數。

注意:

  • 我們只能在構造函數中使用super()和this(),而不能在其他任何地方使用它,任何嘗試都會導致編譯時錯誤。

  • 我們必須保留super()或this()作為構造函數的第一行,但不能同時保留兩者。

超級&此關鍵字

  • super-調用父類成員(變量和方法)。
  • 這-調用相同的類成員(變量和方法)。

注意:我們可以在類中除靜態區域(靜態塊或方法)以外的任何地方使用它們,任何嘗試都會導致編譯時錯誤。

關鍵字用於在同一類中調用構造函數(其他重載的構造函數)

語法this (參數列表); //與同一類中其他構造函數中的args列表兼容

super關鍵字用於在super類中調用構造函數。

語法: super(參數列表); //與超類的構造函數中的args list兼容。

例如:

public class Rect {
int x1, y1, x2, y2;

public Rect(int x1, int y1, int x2, int y2) // 1st constructor 
{ ....//code to build a rectangle }
}

public Rect () {   // 2nd constructor
this (0,0,width,height) // call 1st constructor (because it has **4 int args**), this is another way to build a rectangle 
}


public class DrawableRect extends Rect {

public DrawableRect (int a1, int b1, int a2, int b2) {
super (a1,b1,a2,b2) // call super class constructor (Rect class) 
}
}

this用於訪問當前對象的方法和字段。 因此,它在靜態方法中沒有任何意義。

super允許訪問超類中的非私有方法和字段,並且只能從該類的構造函數中訪問構造函數。

編寫代碼時,通常您不想重復自己。 如果您有一個可以用各種參數構造的類,那么避免重復自己的常見解決方案是簡單地使用缺少參數的默認值調用另一個構造函數。 對此只有一個煩人的限制-它必須是已聲明的構造函數的第一行。 例:

MyClass()
{
   this(default1, default2);
}

MyClass(arg1, arg2)
{
   validate arguments, etc...
   note that your validation logic is only written once now
}

至於super()構造函數,再次不同於super.method()訪問,它必須是構造函數的第一行。 之后,它非常類似於this()構造函數DRY(不要重復自己),如果擴展的類具有一個構造函數,該構造函數可以執行您想要的某些操作,然后使用它,然后繼續構造您的對象,例如:

YourClass extends MyClass
{
   YourClass(arg1, arg2, arg3)
   {
      super(arg1, arg2) // calls MyClass(arg1, arg2)
      validate and process arg3...
   }
}

附加信息:

即使您看不到它,默認的no參數構造函數始終始終先調用super() 例:

MyClass()
{
}

相當於

MyClass()
{
   super();
}

我看到很多人提到在方法和變量上使用thissuper關鍵字-都很好。 請記住,構造函數在用法上有獨特的限制,最值得注意的是,它們必須是已聲明的構造函數的第一條指令,並且只能使用一個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM