star导入是否包含Java中的子包?

Does the star import include subpackages in Java?

当您像这样声明导入时:

import com.microsoft.azure.storage.*;

这是否也包括其子包中的所有内容?例如,它包括这个吗?

import com.microsoft.azure.storage.blob.*

如果不是,为什么不呢? (编辑:"why" 问题基本上是题外话。在考虑正确答案时忽略这一点。)

不,它没有。它只导入包中的所有内容(即目录本身)。子目录被认为是不同的包,因此您需要:

import com.microsoft.azure.storage.*
import com.microsoft.azure.storage.blob.*

至于为什么语言设计者选择走这条路,我们只能猜测,但他们决定采用的系统确实允许采用更细粒度的方法。

是的,您可以从一次导入中导入所有 类,但无法导入具有相似名称的多个包。例如 import java.util*;不也导入 java.util.prefs 或 .jar 你必须单独导入这些。我不知道这是否回答了您的问题,以及为什么我不确定这样做是否有意义。如果您要导入具有相同静态变量的类似包,但您只需要其中的两个或三个包,那么您会收到错误或代码不 运行 正确。

这些有一个名字 - type import on demand

A type-import-on-demand declaration allows all accessible types of a named package or type to be imported as needed.

他们也只导入包本身,而不是任何子包,正如示例所阐明的,强调我的:

import java.util.*;

causes the simple names of all public types declared in the package java.util to be available within the class and interface declarations of the compilation unit. Thus, the simple name Vector refers to the type Vector in the package java.util in all places in the compilation unit where that type declaration is not shadowed (§6.4.1) or obscured (§6.4.2).

does that include everything in / subdirectories too? including something like this?

*代表包com.microsoft.azure.storage内的所有编译单元,其中子包实际上不是编译单元,因此在您编写myPack.*时不会获取。编译单元包括classinterfaceenum