辅助功能
All members of interfaces lacking access modifiers are implicitly public
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
, andfinal
. 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/