java - Java 8 中接口(interface)方法的隐式说明符/修饰符是什么?

标签 java intellij-idea java-8

我知道接口(interface)方法是隐式的publicJava Docs Tutorial

All abstract, default, and static methods in an interface are implicitly public, so you can omit the public modifier.

在 abstract、static 和 default 之外,哪些修饰符在 Java 8 中被隐式声明。

我认为 abstract 不再隐式声明,因为 Java 8 在接口(interface)中引入了 default 方法,但我仍然收到 Intellij IDEA 中的警告 .

Modifier 'abstract' is redundant for interface methods.

public interface TestInterface {
    abstract int print();  // abstract redundant ?.
    int print2(); //legal.but public or public abstract ?.
}

最佳答案

language spec - 特别是第 9.4 节,声明 abstractpublic 是隐式的。

Every method declaration in the body of an interface is implicitly public (§6.6). It is permitted, but discouraged as a matter of style, to redundantly specify the public modifier for a method declaration in an interface.

An interface method lacking a default modifier or a static modifier is implicitly abstract, so its body is represented by a semicolon, not a block. It is permitted, but discouraged as a matter of style, to redundantly specify the abstract modifier for such a method declaration.

这就是 IntelliJ 警告您的原因;通过 JLS,您正在做一些完全多余的事情。

作为奖励,fields in interfaces是隐式的public static final:

Every field declaration in the body of an interface is implicitly public, static, and final. It is permitted to redundantly specify any or all of these modifiers for such fields.

关于java - Java 8 中接口(interface)方法的隐式说明符/修饰符是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48889674/

相关文章:

java - 如何使用单独的类(JPA 注释)和 JAXB 注释并手动进行映射

java - 从过滤器重定向到 JSP 时传递属性 : Session vs Request

java - 使用 GWT 设置数据库

Java 在类方法中抛出异常

android-studio - 我在哪里可以在 Windows 7 上找到ideavim 0.41 的 .ideavimrc 文件?

java - 使用 Stream 实例化新对象

java - 如何阻止 Intellij 自动更改未编辑行的缩进?

intellij-idea - 如何使用 Intellij idea14.0.2 调试 golang?

java - 通过 EL 以人类可读的格式打印 Duration

java - 将双嵌套 for 循环重写为 Java 8 流