The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
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.每种方法都适用于不同的情况,如以下各节所述。
So far, most of the examples in this tutorial have referred to types by their simple names, such as 到目前为止,本教程中的大多数示例都通过简单的名称引用了类型,例如Rectangle
and StackOfInts
. Rectangle
和StackOfInts
。You 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.或者,您可以导入成员或其包,然后使用其简单名称。
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.但是,如果您使用一个包中的许多类型,则应该导入整个包。
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
语句,通常只导入单个包成员或整个包。
import
allows you to import the public nested classes of an enclosing class. import
形式允许您导入封闭类的公共嵌套类。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.DoubleWide
和Rectangle.Square
,则可以使用以下两条语句导入Rectangle
及其嵌套类。import graphics.Rectangle; import graphics.Rectangle.*;
Rectangle
.import
语句不会导入Rectangle
。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编译器会为每个源文件自动导入两个完整的包:(1)java.lang
package and (2) the current package (the package for the current file).Java.lang
包和(2)当前包(当前文件的包)。
At first, packages appear to be hierarchical, but they are not. 起初,包看起来是分层的,但实际上并非如此。For example, the Java API includes a 例如,Java API包括java.awt
package, a java.awt.color
package, a java.awt.font
package, and many others that begin with java.awt
. 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.color
、java.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.*;
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.graphics
和java.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;
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.*;