简体   繁体   English

为什么在本示例中未调用子类方法,因为它在Java中是动态多态性中调用的?

[英]Why child class method is not called in this example as it is called in dynamic polymorphism in Java?

I have done some research but I am no able to find why runtime polymorphism is not taking place in example below. 我已经做过一些研究,但是在下面的示例中我无法找到为什么运行时多态性没有发生的原因。 According to my understanding foo(int a) should have been called in child.But out put is "Parent with long".Can someone throw light on this? 根据我的理解,应该在孩子中调用foo(int a)。但是输出的是“ Parent with long”。有人可以对此加以说明吗?

class Parent {
public void foo() throws RuntimeException {
    System.out.println("Parent with no Parameter");
}

public void foo(long a) {
    System.out.println("Parent with long");
}
}

class Child extends Parent {
public void foo() {
    System.out.println("Child with no parameter");
}

public void foo(int a) throws RuntimeException {
    System.out.println("Child with int");
}
}

class Demo {
public static void main(String... args) {
    Parent p = new Child();
    int a = 10;
    p.foo(a);
}
}

Output: 输出:

Parent with long 父母长

仅当方法具有完全相同的签名(相同数量和类型的参数)时,多态性才适用。

What you have is Parent with 2 methods: foo(long) and foo() . 您拥有的是具有2种方法的Parentfoo(long)foo() And Child that inherits from its parent and add two new different methods: foo(int) and (that one overloads existing methods) foo() (that one overrides the inherited one). 从其父级继承并添加两个新的不同方法的Childfoo(int)和(该方法重载了现有方法) foo() (该方法覆盖了继承的方法)。

There is different mechanisms: what happens at compile time, and what happens at runtime. 有不同的机制:什么在编译时发生,什么在运行时发生。 At compile time, the compiler will only look accordingly to variable types. 在编译时,编译器只会相应地查看变量类型。 At runtime, the execution env. 在运行时,执行环境。 will look at object types. 将查看对象类型。

Now when compiling the call p.foo(a) , compiler will look at the type of p , which is Parent and look in Parent class for a corresponding callable method. 现在,在编译调用p.foo(a) ,编译器将查看p的类型(即Parent并在Parent类中查找相应的可调用方法。 It finds foo(long) and then generates a dynamic call to a method foo(long) with a cast from int to long to the real object at runtime (of type Child ). 它找到foo(long) ,然后在运行时(类型为Child )从intlong转换为真实对象的方法foo(long)的动态调用。 But this object has only one such method, the one in Parent , so this one will be called. 但是此对象只有一个这样的方法, Parent的一个,因此将调用此方法。

Change Child 's foo(int) into foo(long) and you will have what you wanted. Childfoo(int)更改为foo(long) ,您将拥有所需的内容。

As you said polymorphism is correct. 正如您所说,多态性是正确的。 What you seeing is compile time polymorphism which is overloading. 您所看到的是编译时多态性,这是超载的。

The method from Child already been decided at compile time cause there is no equivalent method define in parent and no ovveriding (runtime polymorphism) applied . Child的方法已经在编译时确定,因为在parent中没有定义等效的方法,也没有应用ovveriding(运行时多态)。

In java polymorphism works in down to top mechanism.. you see.. when you are creating the child object, you are actuality creating a patent object.. and it will never give an error as your child extends to patent. 在Java中,多态性从头到尾发挥作用。..您看到..在创建子对象时,实际上是在创建专利对象..当您的孩子扩展到专利时,它将永远不会出错。 So.. you are actually creating a patent object.. not a child object at all.. that's why it's executing parents' method.. if you would like to see how polymorphism works.. then just create Child object =new Child (). 所以..您实际上是在创建一个专利对象..根本不是一个子对象..这就是为什么它执行父母的方法..如果您想了解多态性是如何工作的..则只需创建Child对象= new Child() 。 And then pass Long or int variable.. Hope that helps 然后传递Long或int变量。希望对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM