Documentation

The Java™ Tutorials
Hide TOC
Controlling Access to Members of a Class控制对类成员的访问
Trail: Learning the Java Language
Lesson: Classes and Objects
Section: More on Classes

Controlling Access to Members of a Class控制对类成员的访问

Access level modifiers determine whether other classes can use a particular field or invoke a particular method.访问级别修饰符确定其他类是否可以使用特定字段或调用特定方法。There are two levels of access control:有两个级别的访问控制:

A class may be declared with the modifier public, in which case that class is visible to all classes everywhere.可以使用修饰符public声明一个类,在这种情况下,该类对任何地方的所有类都可见。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.)如果一个类没有修饰符(默认值,也称为包私有),那么它只在它自己的包中可见(包是相关类的命名组—您将在后面的课程中了解它们。)

At the member level, you can also use the public modifier or no modifier (package-private) just as with top-level classes, and with the same meaning.在成员级别,您还可以使用public修饰符或无修饰符(package private),就像使用顶级类一样,并且具有相同的含义。For members, there are two additional access modifiers: private and protected.对于成员,还有两个附加的访问修饰符:privateprotectedThe private modifier specifies that the member can only be accessed in its own class.private修饰符指定只能在成员自己的类中访问该成员。The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.protected修饰符指定成员只能在其自己的包内访问(与package-private一样),此外,还可以由另一个包中其类的子类访问。

The following table shows the access to members permitted by each modifier.下表显示了对每个修改器允许的成员的访问。

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

The first data column indicates whether the class itself has access to the member defined by the access level.第一个数据列指示类本身是否可以访问由访问级别定义的成员。As you can see, a class always has access to its own members.如您所见,类始终可以访问自己的成员。The second column indicates whether classes in the same package as the class (regardless of their parentage) have access to the member.第二列指示与该类位于同一包中的类(无论其父级)是否有权访问该成员。The third column indicates whether subclasses of the class declared outside this package have access to the member.第三列指示在该包外部声明的类的子类是否有权访问该成员。The fourth column indicates whether all classes have access to the member.第四列指示是否所有类都可以访问该成员。

Access levels affect you in two ways.访问级别在两个方面影响您。First, when you use classes that come from another source, such as the classes in the Java platform, access levels determine which members of those classes your own classes can use.首先,当您使用来自其他源的类(例如Java平台中的类)时,访问级别决定您自己的类可以使用这些类的哪些成员。Second, when you write a class, you need to decide what access level every member variable and every method in your class should have.其次,在编写类时,需要确定类中的每个成员变量和每个方法应具有的访问级别。

Let's look at a collection of classes and see how access levels affect visibility.让我们看一组类,看看访问级别如何影响可见性。The following figure shows the four classes in this example and how they are related.下图显示了本例中的四个类以及它们之间的关系。

Classes and Packages of the Example Used to Illustrate Access Levels

Classes and Packages of the Example Used to Illustrate Access Levels用于说明访问级别的示例的类和包

The following table shows where the members of the Alpha class are visible for each of the access modifiers that can be applied to them.下表显示了Alpha类的成员对于每个可应用于它们的访问修饰符可见的位置。

Visibility可见性
Modifier修饰符 Alpha Beta Alphasub Gamma
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N

Tips on Choosing an Access Level:选择访问级别的提示: 

If other programmers use your class, you want to ensure that errors from misuse cannot happen. Access levels can help you do this.如果其他程序员使用您的类,您希望确保不会发生误用导致的错误。访问级别可以帮助您做到这一点。

  • Use the most restrictive access level that makes sense for a particular member.使用对特定成员有意义的最严格访问级别。Use private unless you have a good reason not to.除非你有很好的理由不这样做,否则请使用private
  • Avoid public fields except for constants.避免使用除常量以外的public字段。(Many of the examples in the tutorial use public fields. This may help to illustrate some points concisely, but is not recommended for production code.)(本教程中的许多示例都使用public字段。这可能有助于简要说明某些要点,但不建议用于生产代码。)Public fields tend to link you to a particular implementation and limit your flexibility in changing your code.公共字段往往会将您链接到特定的实现,并限制您更改代码的灵活性。


Previous page: Using the this Keyword
Next page: Understanding Class Members