【发布时间】:2017-08-09 10:35:28
【问题描述】:
假设我有这样的代码:
public interface JustAnInterface {
void doSomething();
}
public class JustAnInterfaceImplementation implements JustAnInterface {
@Override
public static void doSomething() {
}
}
为什么static doSomething() 方法显示错误“方法没有覆盖其超类中的方法”?
【问题讨论】:
-
因为接口中定义的方法是非静态的。你的问题基本上是循环的。
-
静态方法通常没有意义重写。它们属于定义它们的类,并且只属于该类,不构成层次结构的一部分。
-
方法签名与接口不同。
-
因为你不能有两个具有相同签名的方法,一个是静态的,另一个是非静态的。请记住,实际上您正在实现接口,因此需要使用非静态接口,因此
Interface in = new YourImplementation(); in.interfaceMethod();之类的代码将始终有效。 -
静态方法是“早期绑定的”,静态方法调用的实现在编译时是已知的。对接口方法的调用是“后期绑定”的,实现只在运行时才知道
标签: java inheritance static interface-implementation