在java中导入特定包或带有通配符的整个树是否更好? [英] Is it better to import specific packages or a whole tree with wildcards in java?

查看:178
本文介绍了在java中导入特定包或带有通配符的整个树是否更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

为什么使用带有Java导入语句的外卡不好?

现在我使用了很多java.util包:

Right now I'm using a lot of java.util packages:

import java.util.Calendar;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

这样做会更有效:

import java.util.*;

这有什么性能/效率成本?它甚至重要吗?请原谅我对这个问题的无知。

What are the performance/efficiency costs of this? Does it even matter? Please excuse my ignorance on the subject.

此外,这只是一个黑暗的镜头,但有没有办法导入整个项目的包?这样我就不需要在每个类的基础上重新导入它们了?这是我的第一个大型Java项目,所以我还在学习更多的企业方面。

Also, this is just a shot in the dark but is there a way to import packages for a whole project? So that I don't need to re-import them on a per-class basis? This is my first big Java project so I'm still learning the more enterprise side of things.

推荐答案

此外,你可以'包括整棵树。您可以将导入列表减少到

Also, you can't include a whole tree. You can cut down your import list to

import java.util.Calendar;
import java.util.logging.*;

但是

import java.util.*;

不会导入 java.util.logging 包。对于Java编译器,没有子包。也不适用于VM(除了一些使用包结构作为文件目录结构的类加载器)。

does not import anything in the java.util.logging package. For the Java compiler, there are no subpackages. Also not for the VM (apart from some classloaders which use the package structure as a file directory structure).

此外,看来你正在混合概念 package class / interface (或 type )。 java.util java.util.logging 是包,而 java.util.Calendar java.util.logging.Level 等是类。

Also, it seems you are mixing the concepts package and class/interface (or type). java.util and java.util.logging are packages, while java.util.Calendar, java.util.logging.Level etc. are classes.

A类型(类/接口)是您可以在程序中使用的东西,而包主要只是一个名称空间,用于放置类和接口。 (此外,它会对可见性产生一些影响。)

A type (class/interface) is something you can use in your programm, while a package is mainly only a name space in which to put classes and interfaces. (Additionally, it has some consequences on visibility.)

您可以直接在包中导入单个类型(通过指定其名称)或所有类型(通过指定包和。* )。 (您还可以使用通配符导入类型中的所有嵌套类型,或者使用 import static< class>。导入类的所有静态方法/字段,只需完整性)。

You can import either single types (by specifying their name) or all types directly in a package (by specifying the package and .*). (You can also import all nested types in a type by using a wildcard, or import all static methods/fields of a class with import static <class>.*, just for completeness).

导入永远不会递归,通配符导入仅适用于一个级别。 (而且你也不能使用 import java.util。*。* 来导入日志记录类。)

Importing is never recursive, wildcard importing is only for one level. (And you also can't use import java.util.*.* to import the logging classes.)

这篇关于在java中导入特定包或带有通配符的整个树是否更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆