问题描述
如果我们要导入 java.parentpackage.*;
...
If we are importing java.parentpackage.*;
...
- ...子包类是否也被导入或仅父包中的类被导入?
- ...是否需要导入
java.parentpackage.childpackage.*;
来包括所有子包类?
- ... do child package classes also get imported or only the classes in parent package?
- ... is it required to import
java.parentpackage.childpackage.*;
to include all child package classes?
推荐答案
以包名和.**结尾的导入声明.*
仅导入包的所有公共类/接口/注释,不导入任何内容.别的.如果您还需要子包中的类,则必须分别导入它们.
An import declaration which ends with a package name and .*
imports all public classes/interfaces/annotations of the package only and nothing else. If you need classes from the child package too, you have to import them separately.
如果导入声明包含特定的类,则只会导入该类.
If an import declaration contains a specific class, only that class will be imported.
如果导入声明包含特定的类加.*
(这是静态导入,必须采用 import static ...
的形式),则所有静态字段和方法将从该类中导入,仅此而已.
If the import declaration contains a specific class plus .*
(it's a static import, must be in the form of import static ...
), then all static fields and methods will be imported from that class and nothing more.
通常只导入特定的类.这样可以减少类名冲突的可能性(在2个不同的程序包中定义了2个具有相同名称的不同类).
Usually only specific classes are imported. It decreases the chances of class name collisions (2 different classes with same name defined in 2 different packages).
此外,您无需导入类即可使用它,可以使用限定名称,每次引用该类时,都始终写完整的程序包名称和类名称.
Also you are not required to import a class to use it, you can use a qualified name where every time you refer to that class, you always write full package name and class name.
这篇关于导入软件包还会导入子软件包吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!