Java全部导入 [英] Java import all from all
问题描述
我有一个逻辑问题:为什么我无法从java中的所有包中导入所有包?
例如,我可以从java.awt导入所有类:
I have a logical question: Why i cannot import all packages from all packages in java? For example i can import all classes from java.awt:
import java.awt.*;
但以下情况不可能:
import java.awt.*.*;
我的目标是从awt.image和awt.event导入所有内容等等。
有没有其他方法可以做到这一点?
My aim would be to import all stuff from awt.image and awt.event and so on. Is there another way to do this?
谢谢!
推荐答案
无法在Java中实现导入a.package。*。*;
。 JLS,第7.5节指定合法的唯一4种类型的导入:
There is no way to achieve an import a.package.*.*;
in Java. The JLS, Section 7.5 specifies the only 4 types of imports that are legal:
单一类型导入声明(第7.5.1节)导入一个名为类型,
,提及其规范名称(§6.7)。
A single-type-import declaration (§7.5.1) imports a single named type, by mentioning its canonical name (§6.7).
例如 import java.util.List;
按需输入类型声明(第7.5.2节)根据需要导入命名类型或命名包的所有
可访问类型(第6.6节),
提及类型或包的规范名称。
A type-import-on-demand declaration (§7.5.2) imports all the accessible types (§6.6) of a named type or named package as needed, by mentioning the canonical name of a type or package.
例如 import java.awt。*;
单静态导入声明( §7.5.3)通过给出规范的
名称,从类型中导入具有给定名称的所有可访问的
静态成员。
A single-static-import declaration (§7.5.3) imports all accessible static members with a given name from a type, by giving its canonical name.
例如 import static org.junit.Assert.assertEquals;
静态导入 - 按需声明(第7.5.4节)通过提及类型的规范
名称,根据需要导入所有可访问的
命名类型的静态成员。
A static-import-on-demand declaration (§7.5.4) imports all accessible static members of a named type as needed, by mentioning the canonical name of a type.
例如 import static org.junit.Assert。*;
包允许单独引用同名的类。例如。有 java.awt.List
和 java.util.List
。什么会阻止某人使用 java。*。*;
导入所有内容。如何解决 List
呢?会有太多歧义。
Packages allow classes of the same name to be referenced individually. E.g. there is java.awt.List
and java.util.List
. What would stop someone from importing everything with java.*.*;
. How would List
be resolved then? There would be too much ambiguity.
这篇关于Java全部导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!