Documentation

The Java™ Tutorials
Hide TOC
Creating a Package创建包
Trail: Learning the Java Language
Lesson: Packages
Section: Creating and Using Packages

Creating a Package创建包

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语句,它适用于文件中的所有类型。


Note: If you put multiple types in a single source file, only one can be public, and it must have the same name as the source file. 如果在单个源文件中放置多个类型,则只有一个类型可以是public的,并且它必须与源文件具有相同的名称。For example, you can define 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,依此类推。

You can include non-public types in the same file as a public type (this is strongly discouraged, unless the non-public types are small and closely related to the public type), but only the public type will be accessible from outside of the package. 您可以将非公共类型包含在与公共类型相同的文件中(强烈反对这样做,除非非公共类型较小且与公共类型密切相关),但只有公共类型可以从包外部访问。All the top-level, non-public types will be package private. 所有顶级非公共类型都将是包私有的

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.否则,类和接口属于命名包。


Previous page: Creating and Using Packages
Next page: Naming a Package