class Abc{
public static void hello(){
System.out.println("parent");//Line1
}
}
class Abc1 extends Abc{
public void hello(){//Line2
System.out.println("child");//Line3
}
}
编译器在第 3 行给出错误,指出
This instance method cannot override the static method from Abc
为什么静态方法不能被实例方法覆盖?
最佳答案
简单:因为语言规范是这么说的。
这是静态方法的缺点之一:它们没有多态性!从概念上讲,它们不应该被覆盖。这就是全部内容。
准确地说:JLS说静态和非静态方法和状态之间的区别:
An instance method is always invoked with respect to an object, which becomes the current object to which the keywords this and super refer during execution of the method body.
关于java - 为什么实例方法不能重写静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43848140/