且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么我不能在接口中声明静态方法?

更新时间:2021-09-06 04:22:31

这里有几个问题.第一个是声明一个静态方法而不定义它的问题.这就是

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!