Documentation

The Java™ Tutorials
Hide TOC
Using Package Members使用包成员
Trail: Learning the Java Language
Lesson: Packages
Section: Creating and Using Packages

Using Package Members使用包成员

The types that comprise a package are known as the package members.组成包的类型称为包成员

To use a public package member from outside its package, you must do one of the following:要从其包外部使用public包成员,必须执行以下操作之一:

Each is appropriate for different situations, as explained in the sections that follow.每种方法都适用于不同的情况,如以下各节所述。

Referring to a Package Member by Its Qualified Name通过其限定名称引用包成员

So far, most of the examples in this tutorial have referred to types by their simple names, such as Rectangle and StackOfInts. 到目前为止,本教程中的大多数示例都通过简单的名称引用了类型,例如RectangleStackOfIntsYou can use a package member's simple name if the code you are writing is in the same package as that member or if that member has been imported.如果正在编写的代码与某个包成员在同一个包中,或者该成员已导入,则可以使用该包成员的简单名称。

However, if you are trying to use a member from a different package and that package has not been imported, you must use the member's fully qualified name, which includes the package name. 但是,如果您试图使用其他包中的成员,而该包尚未导入,则必须使用该成员的完全限定名,其中包括包名。Here is the fully qualified name for the Rectangle class declared in the graphics package in the previous example.下面是前面示例中graphics包中声明的Rectangle类的完全限定名。

graphics.Rectangle

You could use this qualified name to create an instance of graphics.Rectangle:您可以使用此限定名称创建graphics.Rectangle的实例:

graphics.Rectangle myRect = new graphics.Rectangle();

Qualified names are all right for infrequent use. 限定名称可以不经常使用。When a name is used repetitively, however, typing the name repeatedly becomes tedious and the code becomes difficult to read. 但是,当一个名称被重复使用时,重复键入该名称会变得单调乏味,代码也会变得难以阅读。As an alternative, you can import the member or its package and then use its simple name.或者,您可以导入成员或其包,然后使用其简单名称。

Importing a Package Member导入包成员

To import a specific member into the current file, put an import statement at the beginning of the file before any type definitions but after the package statement, if there is one. 若要将特定成员导入到当前文件中,请在文件开头的任何类型定义之前放置一条import语句,如果有,则放在package语句之后。Here's how you would import the Rectangle class from the graphics package created in the previous section.下面是如何从上一节中创建的graphics包中导入Rectangle类。

import graphics.Rectangle;

Now you can refer to the Rectangle class by its simple name.现在,您可以通过其简单名称来引用Rectangle类。

Rectangle myRectangle = new Rectangle();

This approach works well if you use just a few members from the graphics package. 如果您只使用graphics包中的几个成员,那么这种方法效果很好。But if you use many types from a package, you should import the entire package.但是,如果您使用一个包中的许多类型,则应该导入整个包。

Importing an Entire Package导入整个包

To import all the types contained in a particular package, use the import statement with the asterisk (*) wildcard character.要导入特定包中包含的所有类型,请使用带有星号(*)通配符的import语句。

import graphics.*;

Now you can refer to any class or interface in the graphics package by its simple name.现在,您可以通过graphics包的简单名称来引用graphics包中的任何类或接口。

Circle myCircle = new Circle();
Rectangle myRectangle = new Rectangle();

The asterisk in the import statement can be used only to specify all the classes within a package, as shown here. import语句中的星号只能用于指定包中的所有类,如下所示。It cannot be used to match a subset of the classes in a package. 它不能用于匹配包中类的子集。For example, the following does not match all the classes in the graphics package that begin with A.例如,以下内容与graphics包中以A开头的所有类不匹配。

// does not work
import graphics.A*;

Instead, it generates a compiler error. 相反,它会生成一个编译器错误。With the import statement, you generally import only a single package member or an entire package.使用import语句,通常只导入单个包成员或整个包。


Note: Another, less common form of import allows you to import the public nested classes of an enclosing class. 另一种不太常见的import形式允许您导入封闭类的公共嵌套类。For example, if the graphics.Rectangle class contained useful nested classes, such as Rectangle.DoubleWide and Rectangle.Square, you could import Rectangle and its nested classes by using the following two statements. 例如,如果graphics.Rectangle类包含有用的嵌套类,例如Rectangle.DoubleWideRectangle.Square,则可以使用以下两条语句导入Rectangle及其嵌套类。
import graphics.Rectangle;
import graphics.Rectangle.*;
Be aware that the second import statement will not import Rectangle.请注意,第二条import语句会导入Rectangle

Another less common form of import, the static import statement, will be discussed at the end of this section. 另一种不太常见的import形式,静态导入语句,将在本节末尾讨论。

For convenience, the Java compiler automatically imports two entire packages for each source file: (1) the java.lang package and (2) the current package (the package for the current file).为了方便起见,Java编译器会为每个源文件自动导入两个完整的包:(1)Java.lang包和(2)当前包(当前文件的包)。

Apparent Hierarchies of Packages包的明显层次结构

At first, packages appear to be hierarchical, but they are not. 起初,包看起来是分层的,但实际上并非如此。For example, the Java API includes a java.awt package, a java.awt.color package, a java.awt.font package, and many others that begin with java.awt. 例如,Java API包括Java.awt包、Java.awt.color包、Java.awt.font包以及许多以Java.awt开头的其他包。However, the java.awt.color package, the java.awt.font package, and other java.awt.xxxx packages are not included in the java.awt package. 但是,java.awt包中不包括java.awt.color包、java.awt.font包和其他java.awt.xxxx包。The prefix java.awt (the Java Abstract Window Toolkit) is used for a number of related packages to make the relationship evident, but not to show inclusion.前缀java.awt(java抽象窗口工具包)用于许多相关包,以使关系变得明显,但不显示包含。

Importing java.awt.* imports all of the types in the java.awt package, but it does not import java.awt.color, java.awt.font, or any other java.awt.xxxx packages. 导入java.awt.*导入java.awt包中的所有类型,但不导入java.awt.colorjava.awt.font或任何其他java.awt.xxxx包。If you plan to use the classes and other types in java.awt.color as well as those in java.awt, you must import both packages with all their files:如果计划使用java.awt.color中的类和其他类型以及java.awt中的类和其他类型,则必须导入两个包及其所有文件:

import java.awt.*;
import java.awt.color.*;

Name Ambiguities名称歧义

If a member in one package shares its name with a member in another package and both packages are imported, you must refer to each member by its qualified name. 如果一个包中的成员与另一个包中的成员共享其名称,并且两个包都已导入,则必须通过其限定名称引用每个成员。For example, the graphics package defined a class named Rectangle. 例如,graphics包定义了一个名为Rectangle的类。The java.awt package also contains a Rectangle class. java.awt包还包含一个Rectangle类。If both graphics and java.awt have been imported, the following is ambiguous.如果已导入graphicsjava.awt,则以下内容不明确。

Rectangle rect;

In such a situation, you have to use the member's fully qualified name to indicate exactly which Rectangle class you want. 在这种情况下,必须使用成员的完全限定名来准确指示所需的Rectangle类。For example,例如:

graphics.Rectangle rect;

The Static Import Statement静态导入语句

There are situations where you need frequent access to static final fields (constants) and static methods from one or two classes. 在某些情况下,您需要从一个或两个类频繁访问静态最终字段(常量)和静态方法。Prefixing the name of these classes over and over can result in cluttered code. 反复使用这些类的名称前缀可能会导致代码混乱。The static import statement gives you a way to import the constants and static methods that you want to use so that you do not need to prefix the name of their class.静态导入语句为您提供了一种导入要使用的常量和静态方法的方法,这样您就不需要在它们的类名前面加前缀。

The java.lang.Math class defines the PI constant and many static methods, including methods for calculating sines, cosines, tangents, square roots, maxima, minima, exponents, and many more. java.lang.Math类定义了PI常数和许多静态方法,包括计算正弦、余弦、切线、平方根、最大值、最小值、指数等的方法。For example,例如:

public static final double PI 
    = 3.141592653589793;
public static double cos(double a)
{
    ...
}

Ordinarily, to use these objects from another class, you prefix the class name, as follows.通常,要从另一个类中使用这些对象,需要在类名前加前缀,如下所示。

double r = Math.cos(Math.PI * theta);

You can use the static import statement to import the static members of java.lang.Math so that you don't need to prefix the class name, Math. 您可以使用static import语句来导入java.lang.Math的静态成员,这样就不需要在类名Math前面加前缀。The static members of Math can be imported either individually:Math的静态成员可以单独导入:

import static java.lang.Math.PI;

or as a group:或作为一个组导入:

import static java.lang.Math.*;

Once they have been imported, the static members can be used without qualification. 一旦导入了静态成员,就可以无条件地使用它们。For example, the previous code snippet would become:例如,前面的代码段将变成:

double r = cos(PI * theta);

Obviously, you can write your own classes that contain constants and static methods that you use frequently, and then use the static import statement. 显然,您可以编写自己的类,其中包含经常使用的常量和静态方法,然后使用static import语句。For example,例如:

import static mypackage.MyConstants.*;

Note: Use static import very sparingly. 非常节省地使用静态导入。Overusing static import can result in code that is difficult to read and maintain, because readers of the code won't know which class defines a particular static object. 过度使用静态导入会导致代码难以读取和维护,因为代码的读者不知道哪个类定义了特定的静态对象。Used properly, static import makes code more readable by removing class name repetition. 如果使用得当,静态导入可以通过删除类名重复来提高代码的可读性。

Previous page: Naming a Package
Next page: Managing Source and Class Files