I am reading some Java text and the text says that we can only apply public
or default
access modifier for class and interface. Therefore, it is a compiling error if we declare:
我正在阅读一些Java文本,文本说我们只能为类和接口应用公共或默认访问修饰符。因此,如果我们声明:它是一个编译错误:
private class A {}
or
protected class A{}
I am just curious why a class or an interface cannot receive private
or protected
access modifiers?
我只是好奇为什么类或接口无法接收私有或受保护的访问修饰符?
3 个解决方案
#1
54
private
means "only visible within the enclosing class".
私人意味着“只在封闭的阶级中可见”。
protected
means "only visible within the enclosing class and any subclasses, and also anywhere in the enclosing class's package".
protected表示“仅在封闭类和任何子类中可见,并且也在封闭类的包中的任何位置”。
private
, therefore, has no meaning when applied to a top-level class; the same goes for the first part of the definition of protected
. The second part of protected
could apply, but it is covered by the default (package-protected) modifier, so protected
is part meaningless and part redundant.
因此,私有,在应用于*课程时没有意义;保护的定义的第一部分也是如此。受保护的第二部分可以应用,但它由默认(包受保护的)修饰符覆盖,因此受保护的部分无意义且部分冗余。
Both private
and protected
can be (and frequently are) applied to nested classes and interfaces, just never top-level classes and interfaces.
private和protected都可以(并经常)应用于嵌套类和接口,而不是*类和接口。
#2
15
top level classes can only have public
or default
access, but internal classes can have private access
:
*类只能具有公共访问权限或默认访问权限,但内部类可以具有私有访问权限:
public class TestClassAccess
{
public static void main(String[] args)
{
new TestClassAccess().new TestClassPrivateAccess();
}
private class TestClassPrivateAccess
{
TestClassPrivateAccess()
{
System.out.println("I'm a private class");
}
}
}
#3
5
There are only two use cases for class visibility at the top level (a) Visible everywhere (b) Visible only within the package. Hence only two modifiers (public
and default). If class is public
, then it is visible to all classes. If there is no access modifier, then it is visible only for classes within the same package.
顶层的类可见性只有两个用例(a)可见到处(b)仅在包内可见。因此只有两个修饰符(公共和默认)。如果class是public,那么所有类都可以看到它。如果没有访问修饰符,则它仅对同一包中的类可见。
Had there been more use cases for class visibility at top level, Java language would have provided more modifiers.
如果在顶层有更多用于类可见性的用例,Java语言将提供更多修饰符。
#1
54
private
means "only visible within the enclosing class".
私人意味着“只在封闭的阶级中可见”。
protected
means "only visible within the enclosing class and any subclasses, and also anywhere in the enclosing class's package".
protected表示“仅在封闭类和任何子类中可见,并且也在封闭类的包中的任何位置”。
private
, therefore, has no meaning when applied to a top-level class; the same goes for the first part of the definition of protected
. The second part of protected
could apply, but it is covered by the default (package-protected) modifier, so protected
is part meaningless and part redundant.
因此,私有,在应用于*课程时没有意义;保护的定义的第一部分也是如此。受保护的第二部分可以应用,但它由默认(包受保护的)修饰符覆盖,因此受保护的部分无意义且部分冗余。
Both private
and protected
can be (and frequently are) applied to nested classes and interfaces, just never top-level classes and interfaces.
private和protected都可以(并经常)应用于嵌套类和接口,而不是*类和接口。
#2
15
top level classes can only have public
or default
access, but internal classes can have private access
:
*类只能具有公共访问权限或默认访问权限,但内部类可以具有私有访问权限:
public class TestClassAccess
{
public static void main(String[] args)
{
new TestClassAccess().new TestClassPrivateAccess();
}
private class TestClassPrivateAccess
{
TestClassPrivateAccess()
{
System.out.println("I'm a private class");
}
}
}
#3
5
There are only two use cases for class visibility at the top level (a) Visible everywhere (b) Visible only within the package. Hence only two modifiers (public
and default). If class is public
, then it is visible to all classes. If there is no access modifier, then it is visible only for classes within the same package.
顶层的类可见性只有两个用例(a)可见到处(b)仅在包内可见。因此只有两个修饰符(公共和默认)。如果class是public,那么所有类都可以看到它。如果没有访问修饰符,则它仅对同一包中的类可见。
Had there been more use cases for class visibility at top level, Java language would have provided more modifiers.
如果在顶层有更多用于类可见性的用例,Java语言将提供更多修饰符。