Dmi*_*nov 3 java inheritance static interface-implementation
让我们假设我有这样的代码:
public interface JustAnInterface {
void doSomething();
}
public class JustAnInterfaceImplementation implements JustAnInterface {
@Override
public static void doSomething() {
}
}
Run Code Online (Sandbox Code Playgroud)
为什么static doSomething()方法显示错误" 方法不覆盖其超类方法 "?
有人可能会这样说:因为@Override和static根本就没有合在一起.
请记住:多态性仅适用于非静态方法.static表示您的方法"附加"到包含类.
换句话说:您在编译时了解静态方法调用的所有内容.但是确定调用哪个重写方法的过程在运行时发生.这两个概念在Java中没有结合在一起.