简体   繁体   English

多态性:从超类的变量中调用子类的方法

[英]Polymorphism: calling subclass' method from superclass' variable

In Java, when a method is going to be executed, it's declaration is first searched for at the object's real class (that defines the real type of the object). 在Java中,当要执行一个方法时,首先在对象的真实类(定义对象的真实类型)中搜索其声明。 If it's not found, then the superclass is checked. 如果找不到,则检查超类。 If isn't found there either, the next parent class is checked, and so on. 如果也没有找到,则检查下一个父类,依此类推。 However, look at the example below: 但是,请看下面的示例:

   SuperClass s = new SubClass(list of parameter);
   s.someMethodExistOnlyInSubClass();

This will give me a compile time error. 这将给我一个编译时错误。 I thought that, as mentioned above, it would first look for someMethodExistOnlyInSubClass() at SubClass , verify that it exists there and then execute it right away. 我认为,如上所述,它首先会在SubClass查找someMethodExistOnlyInSubClass() ,确认它在那里存在,然后立即执行它。 If I use a variable of the type SuperClass to reference an instance of SubClass some parameters will go to SuperClass 's constructor through a call to super() and the object will be instantiated there. 如果我使用SuperClass类型的变量来引用SubClass的实例,则某些参数将通过对super()的调用进入SuperClass的构造函数,并且该对象将在此实例化。 In this context, to where have the remaining parameters gone? 在这种情况下,其余参数又去了哪里?

By assigning a reference to an instance of SubClass to a variable of the type SuperClass , the variable is treated as if it were referencing an instance of SuperClass . 通过SuperClass SubClass实例的引用分配给类型为SuperClass的变量,该变量将被视为引用了SuperClass实例。 Therefore, it will have no knowledge of any of the methods declared specifically in SubClass . 因此,它将不了解SubClass专门声明的任何方法。 Note, though, that the overrides performed in SubClass will still be effective. 但是请注意,在SubClass执行的替代仍然有效。

In the context you described, to access someMethodExistOnlyInSubClass() you would have to cast s to the type SubClass . 在您描述的上下文中,要访问someMethodExistOnlyInSubClass() ,必须将s SubClass转换为SubClass类型。 Look below: 往下看:

((SubClass) s).someMethodExistOnlyInSubClass();

What you are trying to do will work only when the method is defined in both Parent class and subclass. 仅当在父类和子类中都定义了该方法时,您要尝试执行的操作才能起作用。

    import java.util.*;
    class Parent 
    {
        public void sample()
        {
            System.out.println("Method of parent is getting Called");
        }
    }


    class Subclass extends Parent
    {
        public void sample()
        {
            System.out.println("Method of Child class is getting Called");
        }
    }

    public class Main
    {
        public static void main(String[] args){
            Parent p = new Subclass();
            p.sample();
        }
    }

OUTPUT : Method of Child class is getting Called

If the method exists only in Child class. 如果该方法仅存在于Child类中。 You will have to create an instance of Child class itself. 您将必须创建Child类本身的实例。

Something like this : 像这样的东西:

import java.util.*;
class Parent 
{

}

class Subclass extends Parent
{
    public void sample()
    {
        System.out.println("Method of Child class is getting Called");
    }
}

public class Main
{
    public static void main(String[] args){
        Subclass p = new Subclass();
        p.sample();
    }
}

Or you can type cast it as mentioned in the earlier post 或者,您可以像之前的文章中所述键入强制转换

Parent p = new Subclass();
((Subclass)p).sample();

An object is created based on its declaring class. 根据其声明类创建对象。 When you declare it as SuperClass s the object s wont have any method called someMethodExistOnlyInSubClass . 当您将其声明为SuperClass s ,对象将没有任何名为someMethodExistOnlyInSubClass方法。 So when you try to invoke this method, even before look at the subclass, you get a compile error. 因此,当您尝试调用此方法时,甚至在查看子类之前,都会遇到编译错误。

The object s contrains only everything of the superclass, this object wont be aware of any subclass until u type cast the object to subclass. 对象s contrains只有超类的一切,这个对象不会知道任何的子类,直到U型投的对象子类。

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

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