Java 9嵌套(分层或父/子)模块 [英] Java 9 Nested (Hierarchical or Parent/Child) Modules
问题描述
是否可以在Java 9中创建基于继承的(或嵌套的)模块?
Is it possible to create inheritance-based (or nested) modules in Java 9?
这样的事情:
module a
|
├── module a1
|
└── module a2
在这个例子中, a1
和 a2
模块是 a
的子项。
In this example, both the a1
and a2
modules are children of a
.
如果我导入其中一个子模块,我将获得父模块的功能( a
)以及该子模块中定义的任何功能。也就是说,我可以导入 a1
,并明确获得对 a
和 a1
(但不是 a2
)。
If I import either of the child modules, I would get the functionality of the parent (a
) along with any functionality defined in that child. That is, I could import a1
, and explicitly gain access to the functionality of both a
and a1
(but not a2
).
两者 a1
和 a2
是 a
,可以访问<$的所有包c $ c> a ,无需向他们公开 a
的包裹。
Both a1
and a2
are an a
, and can access all of the packages of a
, without having to expose the packages of a
to them.
推荐答案
您可以通过的组合模仿您要求的内容
和导出到
语法:
You can emulate what you asked for with a combination of requires transitive
and exports to
syntax:
-
需要传递
:指定导入此模块将固有地导入另一个模块。您可以使用它来确保a1
和a2
导致a
要导入。 -
导出到
:这样您就可以将功能导出到特定模块;这将允许您让a
授予a1
和a2 $ c $的访问权限c>公开出口会很危险的功能。
requires transitive
: Specifies that importing this module will inherently import another module. You can use this to ensure thata1
anda2
causea
to be imported.exports to
: This will let you export functionality to a specific module only; this will allow you to leta
give access toa1
anda2
functionality that would be dangerous to have publicly exported.
所以,例如:
module a {
exports com.internal to a1;
exports com.internal to a2;
}
module a1 {
requires transitive a;
}
module a2 {
requires transitive a;
}
在这种情况下,如果消费者依赖 a1
,它们本来就依赖于 a
(从而获得两者的效用),以及 com.internal
包,虽然可供 a1
供内部使用,但消费者无法在外部看到。
In this case, if a consumer depends on a1
, they will inherently depend on a
(thus gaining the utilities of both), and the com.internal
package, while visible to a1
for internal use, will not be externally visible to the consumer.
这不是你要求的。 a1
不 a
。此外, a1
不能 a
; JLS 不允许对于出口中的通配符(IE: exports * to a1
,仍然不会使 a1
a
但是从实用的角度来看会更接近它,并且模块中不存在允许它的其他语法。
It is not quite what you asked for. a1
is not an a
. Further, a1
can not be made to be an a
; the JLS does not allow for wildcards in exports (IE: exports * to a1
, which still wouldn't make a1
an a
but would bring it closer from a pragmatic perspective), and no other syntax exists in modules to allow for it.
这篇关于Java 9嵌套(分层或父/子)模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!