简体   繁体   English

多态和静态方法

[英]Polymorphism and Static Methods

I have a question about this code right here我对这里的代码有疑问

public Car {
    public static void m1(){
        System.out.println("a");
    }
    public void m2(){
        System.out.println("b");
    }
}

class Mini extends Car {
    public static void m1() {
        System.out.println("c");
    }
    public void m2(){
        System.out.println("d");
    }
    public static void main(String args[]) {
        Car c = new Mini();
        c.m1();
        c.m2();       
   }
}

I know that polymorphism does not work with static methods, only to instance methods.我知道多态性不适用于静态方法,仅适用于实例方法。 And also that overriding doesn't work for static methods.而且这种覆盖不适用于静态方法。

Therefore I think that this program should print out: c, d因此我认为这个程序应该打印出:c, d

Because c calls the m1 method, but it's static, so it can't override and it calls the method in class Mini instead of Car.因为 c 调用了 m1 方法,但它是静态的,所以它不能覆盖,它调用 Mini 类中的方法而不是 Car 。

Is this correct?这样对吗?

However, my textbook says that the answer should be : a, d但是,我的课本上说答案应该是:a, d

is it a typo?这是一个错字吗? Because I'm a little confused right now.因为我现在有点迷茫。

Please clear this up, thanks.请澄清一下,谢谢。

Because c calls the m1 method, but it's static, so it can't override and it calls the method in class Mini instead of Car.因为 c 调用了 m1 方法,但它是静态的,所以它不能覆盖,它调用 Mini 类中的方法而不是 Car 。

That's exactly backwards.那完全是倒退。

c is declared as Car , so static method calls made through c will call methods defined by Car . c声明Car ,因此通过c进行的静态方法调用将调用Car定义的方法。
The compiler compiles c.m1() directly to Car.m1() , without being aware that c actually holds a Mini .编译器将c.m1()直接编译为Car.m1() ,而没有意识到c实际上拥有一个Mini

This is why you should never call static methods through instance like that.这就是为什么你永远不应该通过这样的实例调用静态方法。

I faced the same issue while working with Inheritance.我在使用继承时遇到了同样的问题。 What I have learned is if the method being called is Static then it will be called from the class to which the reference variable belongs and not from the class by which it is instantiated.我学到的是,如果被调用的方法是静态的,那么它将从引用变量所属的类中调用,而不是从实例化它的类中调用。

 public class ParentExamp
    {                   
     public static void Displayer()
     {
      System.out.println("This is the display of the PARENT class");
     }
    }

     class ChildExamp extends ParentExamp
    {
        public static void main(String[] args)
        {
          ParentExamp a  = new ParentExamp();
          ParentExamp b  = new ChildExamp();
          ChildExamp  c  = new ChildExamp();

          a.Displayer(); //Works exactly like ParentExamp.Displayer() and Will 
                        call the Displayer method of the ParentExamp Class

          b.Displayer();//Works exactly like ParentExamp.Displayer() and Will 
                        call the Displayer method of the ParentExamp Class

          c.Displayer();//Works exactly like ChildExamp.Displayer() and Will 
                        call the Displayer method of the ChildtExamp Class
        }               
        public static void Displayer()
        {
         System.out.println("This is the display of the CHILD class");
        }   
    }

在此处输入图片说明

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

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