为什么我不能在接口中声明静态方法? [英] Why can't I declare static methods in an interface?
问题描述
主题大部分说明 - 静态方法无法在界面中声明的原因是什么?
The topic says the most of it - what is the reason for the fact that static methods can't be declared in an interface?
public interface ITest {
public static String test();
}
上面的代码给出了以下错误(至少在Eclipse中): 接口方法ITest.test()的非法修饰符;只允许使用public& abstract。
The code above gives me the following error (in Eclipse, at least): "Illegal modifier for the interface method ITest.test(); only public & abstract are permitted".
推荐答案
有一个在这里玩的几个问题。第一个是在不定义静态方法的情况下声明静态方法的问题。这是
There are a few issues at play here. The first is the issue of declaring a static method without defining it. This is the difference between
public interface Foo {
public static int bar();
}
和
public interface Foo {
public static int bar() {
...
}
}
第一个是不可能的,因为 Espo 提到:你不知道哪个实现类是正确的定义。
The first is impossible for the reasons that Espo mentions: you don't know which implementing class is the correct definition.
Java 可以允许后者;实际上,从Java 8开始,确实如此!
Java could allow the latter; and in fact, starting in Java 8, it does!
这篇关于为什么我不能在接口中声明静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!