为什么 java 包不能按层次结构工作?

Why java packages do not work as a hierarchy?

为什么 java 不允许访问父包中的包限制类型?

像这样的东西:

Package a
|--Class A
|--Package b
   |--Class B

如果 class A 具有包可见性,则无法从 class B 访问 class A。我认为这是有道理的,但显然没有。

这是什么原因?

没有父包或子包这样的东西。包 不是 分层的东西。每个包都是独立的。遵循分层命名模式的包也分层存储在分层文件系统中,这只是约定俗成。

来自JLS Sec 7.1

The hierarchical naming structure for packages is intended to be convenient for organizing related packages in a conventional manner, but has no significance in itself other than the prohibition against a package having a subpackage with the same simple name as a top level type (§7.6) declared in that package.

For example, there is no special access relationship between a package named oliver and another package named oliver.twist, or between packages named evelyn.wood and evelyn.waugh. That is, the code in a package named oliver.twist has no better access to the types declared within package oliver than code in any other package.