簡體   English   中英

Java 繼承 - 調用超類方法

[英]Java Inheritance - calling superclass method

假設我有以下兩個類

public class alpha {

    public alpha(){
        //some logic
    }

    public void alphaMethod1(){
        //some logic
    }
}

public class beta extends alpha {

    public beta(){
        //some logic
    }

    public void alphaMethod1(){
        //some logic
    }
}

public class Test extends beta
{
     public static void main(String[] args)
      {
        beta obj = new beta();
        obj.alphaMethod1();// Here I want to call the method from class alpha.
       }
}

如果我啟動一個新的 beta 類型對象,我如何執行在類 alpha 中找到的alphamethod1邏輯而不是 beta? 我可以只使用super().alphaMethod1() <- 我想知道這是否可能。

Eclipse IDE 中的 Autotype 為我提供了從 class alpha或 class beta選擇alphamethod1的選項。

你可以做:

super.alphaMethod1();

請注意, super是對父級的引用,而 super() 是它的構造函數。

只需使用super.alphaMethod1();

在java中查看super關鍵字

您不能使用 beta 的對象調用 alpha 的 alphaMethod1() 但是您有兩種解決方案:

溶液1:呼叫阿爾法的alphaMethod1()選自β-的alphaMethod1()

class Beta extends Alpha
{
  public void alphaMethod1()
  {
    super.alphaMethod1();
  }
}

或來自 Beta 的任何其他方法,例如:

class Beta extends Alpha
{
  public void foo()
  {
     super.alphaMethod1();
  }
}

class Test extends Beta 
{
   public static void main(String[] args)
   {
      Beta beta = new Beta();
      beta.foo();
   }
}

解決方案 2 :創建 alpha 的對象並調用 alpha 的alphaMethod1()

class Test extends Beta
{
   public static void main(String[] args)
   {
      Alpha alpha = new Alpha();
      alpha.alphaMethod1();
   }
}

可以使用 super 從母類調用該方法,但這意味着您可能有設計問題。 也許B.alphaMethod1()不應該覆蓋 A 的方法並被稱為B.betaMethod1()

如果視情況而定,您可以放置​​一些代碼邏輯,例如:

public void alphaMethod1(){
    if (something) {
        super.alphaMethod1();
        return;
    }
    // Rest of the code for other situations
}

像這樣它只會在需要時調用 A 的方法,並且對類用戶保持不可見。

每當您創建子類對象時,該對象都具有父類的所有功能。 這里 Super() 是加入父級的工具。

如果你當時寫 super() ,則調用父母的默認構造函數。 如果你寫 super 也是一樣。

this 關鍵字指的是與訪問父項的超級關鍵字工具相同的當前對象。

解決方案在此答案的末尾,但在閱讀它之前,您還應該閱讀它之前的內容


您正在嘗試做的事情會通過允許跳過在覆蓋方法中添加的可能驗證機制來破壞安全性。

現在讓我們想象一下我們可以通過像這樣的語法從超類調用方法的版本

referenceVariable.super.methodName(arguments)

如果我們有這樣的課程

class ItemBox{ //can sore all kind of Items
    public void put(Item item){
        //(1) code responsible for organizing items in box 
    }

    //.. rest of code, like container for Items, etc.
}

class RedItemsBox extends ItemBox {//to store only RED items

    @Override
    public void put(Item item){ //store only RED items
        if (item.getColor()==Color.RED){
            //(2) code responsible for organizing items in box
        }
    }
}

正如您所看到的RedItemsBox應該只存儲 RED 項目。

無論我們使用以下哪個

ItemBox     box = new RedItemsBox();
RedItemsBox box = new RedItemsBox();

打電話

box.put(new BlueItem());

將從RedItemsBox調用put方法(因為多態)。 所以它會正確地防止BlueItem對象被放置在RedItemBox

但是如果我們可以使用像box.super.put(new BlueItem())這樣的語法會發生什么

在這里(假設它是合法的)我們將從ItemBox類中執行put方法的版本。
但是該版本沒有負責驗證項目顏色的步驟。 這意味着我們可以將任何Item放入RedItemBox

這種語法的存在意味着在子類中添加的驗證步驟可以隨時被忽略,使它們變得毫無意義。


在某些情況下,執行“原始”方法的代碼是有意義的。

而那個地方是在覆蓋方法里面

請注意,來自 ItemBox 和 RedItemBox 的put方法的注釋//(1) ..//(2)..非常相似。 實際上它們代表相同的動作......

因此,在覆蓋方法中重用“原始”方法中的代碼是有意義的。 可以通過super.methodName(arguments)調用(例如從 RedItemBox 的內部put調用):

@Override
public void put(Item item){ //store only RED items
    if (item.getColor()==Color.RED){
        super.put(item); // <<<--- invoking code of `put` method 
                         //        from ItemBox (supertype)
    }
}

beta obj = 新 beta(); 由於您已經創建了 beta 對象,因此您不能直接引用 alpha 對象的 alphamethod1 。 可以修改為

class Beta extends Alpha
{
  public void alphaMethod1()
  {
    super.alphaMethod1();
  }
}

暫無
暫無

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

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