当前位置: 首页 > 面试题库 >

为什么这两个代码样本产生不同的输出?

高海阳
2023-03-14
问题内容

范例1:

 class Animal {
     public static void saySomething() { System.out.print(" Gurrr!"); 
   }
 }
 class Cow extends Animal {
    public static void saySomething() { 
     System.out.print(" Moo!"); 
    }
    public static void main(String [] args) {
         Animal [] animals = {new Animal(), new Cow()};
         for( Animal a : animals) {
           a.saySomething();
         }
         new Cow().saySomething();
    }
 }

输出为:

 Gurrr! Gurrr! Moo!

范例2:

 class Animal {
     public void saySomething() { System.out.print(" Gurrr!"); 
   }
 }
 class Cow extends Animal {
    public void saySomething() { 
     System.out.print(" Moo!"); 
    }
    public static void main(String [] args) {
         Animal [] animals = {new Animal(), new Cow()};
         for( Animal a : animals) {
           a.saySomething();
         }
         new Cow().saySomething();
    }
 }

输出:

 Gurrr! Moo! Moo!

我只是不明白为什么将saySomething设为非静态会导致第二次调用saySomething调用Cow版本而不是Animal版本。我的理解是,
Gurrr! Moo! Moo!这两种情况都是输出。


问题答案:

静态方法在编译时绑定到其类,并且不能多态使用。在Animal上声明“静态”方法时,该方法永远绑定到Animal类,并且不能被覆盖。静态方法绑定到Class对象,而不是Class的实例。

常规方法在运行时绑定,因此JVM可以查看您对“
saySomething”的调用,并尝试确定您是否正在传递Animal的子类,如果是,则将其重写该saySomething()方法。常规方法绑定到对象的实例,而不是Class本身。

这也是为什么您永远无法做到这一点的原因:

class Animal
{
   public abstract static void saySomething();
}

由于“静态”的意思是“在编译时绑定”,因此将某事物静态化和抽象化是毫无意义的。



 类似资料: