package-private
在官方的指导中这样描述java类的访问级别:
At the top level—public, or package-private (no explicit modifier).
At the member level—public, private, protected, or package-private (no explicit modifier).
不知道package-private是什么,于是继续往下看,看到了这样的一段话:
A class may be declared with the modifier public, in which case that class is visible to all classes everywhere. If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes — you will learn about them in a later lesson.)
大概的意思是,当一个类没有加任何访问修饰符的时候,他的默认访问级别就是package-private,即对其所在包内的其他类是可见的,对包外的类是不可见的,也就是我们常说的package级别。
Access Levels
Modifier |
Class | Package | Subclass | World |
public | Y | Y | Y | Y |
protected | Y | Y | Y | N |
no modifier | Y | Y | N | N |
private | Y | N | N | N |
时间: 2024-11-10 00:37:55