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发行说明。
To create a package, you choose a name for the package (naming conventions are discussed in the next section) and put a 要创建包,请为包选择一个名称(命名约定将在下一节中讨论),并在包含要包含在包中的类型(类、接口、枚举和注释类型)的每个源文件的顶部放置一个具有该名称的package
statement with that name at the top of every source file that contains the types (classes, interfaces, enumerations, and annotation types) that you want to include in the package.package
语句。
The package statement (for example, 包语句(例如,package graphics;
) must be the first line in the source file. package graphics;
)必须是源文件中的第一行。There can be only one package statement in each source file, and it applies to all types in the file.每个源文件中只能有一个package
语句,它适用于文件中的所有类型。
public
, and it must have the same name as the source file. public
的,并且它必须与源文件具有相同的名称。public class Circle
in the file Circle.java
, define public interface Draggable
in the file Draggable.java
, define public enum Day
in the file Day.java
, and so forth.Circle.java
中定义public class Circle
,在文件Draggable.java
中定义public interface Draggable
,在文件Day.java
中定义public enum Day
,依此类推。If you put the graphics interface and classes listed in the preceding section in a package called 如果将上一节中列出的图形界面和类放入名为graphics
, you would need six source files, like this:graphics
的包中,则需要六个源文件,如下所示:
//in the Draggable.java file package graphics; public interface Draggable { . . . } //in the Graphic.java file package graphics; public abstract class Graphic { . . . } //in the Circle.java file package graphics; public class Circle extends Graphic implements Draggable { . . . } //in the Rectangle.java file package graphics; public class Rectangle extends Graphic implements Draggable { . . . } //in the Point.java file package graphics; public class Point extends Graphic implements Draggable { . . . } //in the Line.java file package graphics; public class Line extends Graphic implements Draggable { . . . }
If you do not use a 如果不使用package
statement, your type ends up in an unnamed package. package
语句,则类型将以未命名的包结束。Generally speaking, an unnamed package is only for small or temporary applications or when you are just beginning the development process. 一般来说,未命名的包仅适用于小型或临时应用程序,或者当您刚刚开始开发过程时。Otherwise, classes and interfaces belong in named packages.否则,类和接口属于命名包。