gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:09:19 25 4
gpt4 key购买 nike

我知道接口(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com