OOPS 概念 调用父类而不是子类的函数
我有一个家长班。
import java.util.HashMap;
import java.util.Map;
public class Parent {
Map<String,String> map = new HashMap<String, String>();
public void process(){
System.out.println("parent");
this.checkFunction();
}
protected void checkFunction(){
System.out.println("parentC");
System.out.println(map);
}
public void init(){
(map).put("parent","b");
}
}
现在,正如预期的那样,我有一个儿童班。
import java.util.HashMap;
import java.util.Map;
public class Child extends Parent {
Map<String,String> map = new HashMap<String, String>();
public void checkFunction(){
System.out.println(map);
System.out.println("ChildC");
}
public void process(){
super.process();
System.out.println("Child");
}
public void init(){
super.init();
(map).put("child","b");
}
}
为了测试我想要的,我有一个主课。
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Child a = new Child();
a.init();
a.process();
Parent p = a;
p.checkFunction();
}
}
当我调用 a.process() 时,我假设它应该调用 child.process() ,而 child.process() 将反过来调用 super.process() 。到目前为止,一切都很好。在Parent的process()中,应该调用checkFunction()。
现在,根据我的理解,它应该调用父类的 checkFunction() 。为什么要调用Child的checkFunction()?
我的输出是这样的
parent
{child=b}
ChildC
Child
{child=b}
ChildC
我希望它是
parent
parentC
{parent=b}
Child
{child=b}
ChildC
I have a Parent class.
import java.util.HashMap;
import java.util.Map;
public class Parent {
Map<String,String> map = new HashMap<String, String>();
public void process(){
System.out.println("parent");
this.checkFunction();
}
protected void checkFunction(){
System.out.println("parentC");
System.out.println(map);
}
public void init(){
(map).put("parent","b");
}
}
Now, as expected I have a child class.
import java.util.HashMap;
import java.util.Map;
public class Child extends Parent {
Map<String,String> map = new HashMap<String, String>();
public void checkFunction(){
System.out.println(map);
System.out.println("ChildC");
}
public void process(){
super.process();
System.out.println("Child");
}
public void init(){
super.init();
(map).put("child","b");
}
}
To test what I want, I have a main class.
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Child a = new Child();
a.init();
a.process();
Parent p = a;
p.checkFunction();
}
}
When I call a.process(), I assume it should call child.process() which will in return call super.process(). So far so good. In Parent's process(), it should call checkFunction().
Now, as per my understanding it should call checkFunction() of Parent class. Why is it calling Child's checkFunction()?
My output is something like this
parent
{child=b}
ChildC
Child
{child=b}
ChildC
I expect it to be
parent
parentC
{parent=b}
Child
{child=b}
ChildC
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您在子类中重写了 checkFunction。因此,即使调用来自父类,child 的实例也会调用子 checkFunction。
如果你想从子类的实例调用父类的 checkfunction,你需要在子类中调用 super.checkFunction() 。 super 关键字本质上是“向上移动”继承链。
super 关键字的详细说明可以在此处找到
This is because you have overridden checkFunction in the child class. therefore an instance of child will call the child checkFunction even if the call is from parent class.
If you want to call the parents checkfunction from an instance of the child you need to call super.checkFunction() in the child class. The super keyword essentially "moves up" the inheritance chain.
A detailed description of the super keyword can be found here