簡體   English   中英

在Java中使用super方法打印所有繼承的值

[英]print all inherited values with a super method in Java

package supertest;
public class Parent {
    public void show  (){
        System.out.println(" I am father");     
    }


package supertest;
public class Child  extends Parent{
    public void show(){
        System.out.println("I am child ");
    }
}




public class GrandChild extends Child{

    public void test(){
        super.show();
        System.out.println("I am grand child");

    }




    public static void main(String[] args) {      
        GrandChild gr=new GrandChild();
        gr.test();   

    }

超級方法在輸出中顯示“我是孩子,我是孫子”,但是我想打印所有三個方法值“我是父親,我是孩子,我是孫子”如何解決此問題

grandChild.show() ,調用super.show()將調用child.show()

因此,在child.show() ,需要顯式調用super.show()來調用parent.show()

public class Child  extends Parent{
    public void show(){
        super.show();
        System.out.println("I am child ");
    }
}

暫無
暫無

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

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