Java语言规范基于JavaSE9 第七章 包和模块(一)

来源:互联网 发布:詹姆斯本赛季盖帽数据 编辑:程序博客网 时间:2024/06/07 05:00

PROGRAMS are organized as sets of packages. The members of a package (§7.1) are class and interface types, which are declared in compilation units of the package, and subpackages, which may contain compilation units and subpackages of their own.

程序被组织成包的集合,包的成员(第7.1节)包括在包的编译单元中声明的类和接口类型、包含编译单元的子包、以及拥有自己子包的子包 。

Each package has its own set of names for types, which helps to prevent name conflicts. The naming structure for packages is hierarchical.

每个包都有其用于类型的名字集合,这有助于防止名字冲突。包的命名结构是层次化的。

If a set of packages is sufficiently cohesive, then the packages may be grouped into a module. A module categorizes some or all of its packages as exported, which means their types may be accessed from code outside the module. If a package is not exported by a module, then only code inside the module may access its types. Furthermore, if code in a module wishes to access the packages exported by another module, then the first module must explicitly depend on the second module. Thus, a module controls how its packages use other modules (by specifying dependences) and controls how other modules use its packages (by specifying which of its packages are exported).

可以把一组足够内聚的包分组到一个模块中。若模块将其部分或全部包指定成导出,则意味着这些包的类型可以通过该模块之外的代码被访问;而在模块中没被指定成导出的包,则只能通过模块内的代码访问它们的类型。此外,如果模块中的代码希望访问另一个模块指定成导出的包,则第一个模块必须明确声明依赖于第二个模块。因此,模块可以控制自己的包如何使用其他的模块(通过指定相关性),也可以控制其他的模块如何使用本模块的包(通过指定哪个包被导出)。

Modules and packages may be stored in a file system or in a database (§7.2). Modules and packages that are stored in a file system may have certain constraints on the organization of their compilation units to allow a simple implementation to find module and type declarations easily.

模块和包可以在文件系统或数据库中存储(第7.2节)。存储在文件系统中的模块和包可以对其编译单元的组织结构进行限制,以便实现可以快速地找到模块和类型的声明。

Code in a compilation unit automatically has access to all types declared in its package and also automatically imports all of the public types declared in the predefined package java.lang.

编译单元中的代码自动具有对它所在包中声明的所有类型的访问权限,并且还自动导入了在预定义包java.lang中声明的所有public类型。

A top level type is accessible (§6.6) outside the package that declares it only if the type is declared public. A top level type is accessible outside the module that declares it only if the type is declared public and is a member of an exported package. A type that is declared public but is not a member of an exported package is accessible only to code inside the module.

顶层类型在声明它的包的外部是可访问的(第6.6 节),当且仅当该类型被声明为public。另外,顶层类型在声明它的模块的外部也是可访问的,当且仅当该类型被声明为public并且它是被指定为导出包的成员。而声明为public但不是被指定为导出包的成员的类型则只能在模块内部进行访问。

For small programs and casual development, a package can be unnamed (§7.4.2) or have a simple name, but if code is to be widely distributed, unique package names should be chosen using qualified names. This can prevent the conflicts that would otherwise occur if two development groups happened to pick the same package name and these packages were later to be used in a single program.

对于小型程序和非正式的开发来说,包可以不具名(第7.4.2节)或者具有简单名,但是如果代码需要广泛发布,就应该使用限定名来选定唯一包名。这可以防止两个开发组碰巧选择了相同的包名,从而导致这些包随后在单个程序中使用时所产生的冲突。

原创粉丝点击