[英]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();
}
The super method prints in output "I am child I am grand child" but I would like to print all three method values "I am father, I am child, I am grand child" How can I fix this 超级方法在输出中显示“我是孩子,我是孙子”,但是我想打印所有三个方法值“我是父亲,我是孩子,我是孙子”如何解决此问题
In grandChild.show()
, calling super.show()
will invoke child.show()
. 在grandChild.show()
,调用super.show()
将调用child.show()
。
So, in child.show()
, you need call super.show()
explictly to invoke parent.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.