【发布时间】:2016-10-12 14:54:40
【问题描述】:
我有两个类 Parent 和 Child。从 Child 类我调用父类重写的方法 (show)。从 Parent 类,我调用另一个方法 (display),但由于调用了 Child 方法,该方法也被重写. 我想从 show 方法调用 Parent 方法显示。
public class Parent {
public void show()
{
System.out.println("Show of parent ");
this.display();
}
public void display()
{
System.out.println("Display of parent");
}
}
public class Child extends Parent{
public void show()
{
System.out.println("Show of child ");
super.show();
}
public void display()
{
System.out.println("Display of child");
}
public static void main(String[] args) {
Parent obj = new Child();
obj.show();
}
}
输出:
Show of child
Show of parent
Display of child
需要:
Show of child
Show of parent
Display of parent
即
我想从同一类的show()方法调用Parent类的display()方法
【问题讨论】:
-
你在节目中做到了,但没有在展示中......为什么?从显示器调用 super.display()。
-
@AxelH 我想从同一个类的 show() 方法中调用 Parent 类的 display() 方法
标签: java inheritance polymorphism