Java 9 中接口方法的默认访问修饰符?

Default access modifier for interface methods in Java 9?

Java 9 允许我们在接口中有private个方法,这意味着不显式标记public个方法不再是多余的。

但是,现在是否必须这样做?我希望规范仍然假定 public abstract 作为方法的默认修饰符以保持与早期源代码的向后兼容性?

Java 9 语言规范在 §9.4::

中说

A method in the body of an interface may be declared public or private (§6.6). If no access modifier is given, the method is implicitly public. It is permitted, but discouraged as a matter of style, to redundantly specify the public modifier for a method declaration in an interface.

不幸的是,我找不到不会导致 a PDF, diffing the old and new JLS 的 link。

我学到的东西:

接口中的所有成员都是隐式的 public 并且不能声明 与任何其他访问修饰符一起使用,除非在下面指定:

  • 字段和所有变量都是 public static final implicitly
  • 方法签名,默认方法,(自 Java 8 起允许)使用 'default' 修饰符声明。
  • 静态方法(自 Java 8 起允许)
  • 私有方法(自 Java 9 起允许)静态和非静态具体方法都可以是私有的。
  • 嵌套类型。
  • 方法体仅适用于默认方法、私有方法和静态方法。

来源:Tim Buschalka 的 Learn Programming Academy

还有一个非常清楚但有点长的解释:https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html