Java語言規範基於JavaSE9 第七章 包和模塊(一)

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節)或者具有簡單名,但是如果代碼需要廣泛發佈,就應該使用限定名來選定唯一包名。這可以防止兩個開發組碰巧選擇了相同的包名,從而導致這些包隨後在單個程序中使用時所產生的衝突。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章