我们可以覆盖Java中的静态方法吗?

问题描述:


可能重复:

为什么静态方法在Java中是抽象的?

静态方法及其重写

为什么没有Java允许覆盖静态方法?

我们可以覆盖Java中的静态方法吗?

Can we override static method in Java?

否。静态方法与它们定义的类相关联。它们是通过类调用的,而不是通过对象调用的,并且没有动态调度可能发生重写。

No. Static methods are tied to the class they're defined in. They're invoked through the class, not through an object, and there is no dynamic dispatch where overriding could happen.

您可能感到困惑,因为Java允许您通过对象引用调用静态方法。这通常被认为是设计错误,并且它不像调用实例方法那样工作,因为引用的编译时类型决定调用哪个方法。

You're probably confused because Java allows you to invoke static methods through an object reference. That's generally considered a design error, and it does not work like invoking instance methods, because the compile-time type of the reference decides which method gets invoked.