gpt4 book ai didi

Java 8 : Accessibility of Interface members

转载 作者:搜寻专家 更新时间:2023-11-01 01:52:24 26 4
gpt4 key购买 nike

辅助功能

Java 8 Says:

All members of interfaces lacking access modifiers are implicitly public

Java 7 says:

All members of interfaces are implicitly public.

当我尝试以下代码时:

public interface Test {
protected int i = 10;
}

并编译,我得到了

Test.java:3: error: modifier protected not allowed here        protected int i = 10;                      ^

Java 版本: Java(TM) SE Runtime Environment (build 1.8.0-b129)

但是在上面的 Interface 成员声明中我不缺少访问修饰符 那么为什么我会收到这个错误。

最佳答案

您引用的是 Determining Accessibility with Names 上的 JLS 部分。

All members of interfaces lacking access modifiers are implicitly public

您真正需要查看的是关于 interface field declarations 的部分哪个州

ConstantDeclaration:
{ConstantModifier} UnannType VariableDeclaratorList ;
ConstantModifier:
Annotation 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.

所以这些是您可以使用的修饰符。您在上面引用的部分指的是当您不使用任何访问修饰符时,在这种情况下它将隐式为 public

关于Java 8 : Accessibility of Interface members,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22870346/

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