使用单个语句更方便,更简洁,比如

import java.awt.*;

而不是导入一堆单独的类

import java.awt.Panel;
import java.awt.Graphics;
import java.awt.Canvas;
...

在import语句中使用通配符有什么问题?


当前回答

在DDD书中

在实现将基于的任何开发技术中,寻找最小化的方法 重构模块的工作。在Java中,无法逃避导入到单个类中,只能逃避导入到您 一次至少可以导入整个包,以反映包是高度内聚单元的意图吗 同时减少了更改包名的工作量。

如果它弄乱了本地命名空间,那不是你的错——是包的大小造成的。

其他回答

导入包中的所有类被认为是一种盲目的方法。这样做的一个主要原因是,它会使类名称空间变得混乱,并可能导致具有相同名称的不同包中的类之间发生冲突。

具体地填充必要的类可以避免这个问题,并清楚地显示需要哪些版本。这有利于代码的可维护性。

为什么在Java导入语句中使用通配符是不好的?

如果你正在使用一个IDE(这是你应该做的),并且有比你更多的代码所有者,使用通配符导入是不好的,因为它:

对团队其他成员隐瞒信息 提供给您的只是虚假的好处(使用IDE功能比使用通配符导入更好地解决的事情)

大多数“使用通配符”的支持者都专注于个人:我不想维护列表,我不想看到混乱等等。下面是几个常见的例子:

维护更加困难——当您想要在源代码中引入一个新类时,您必须手动添加import语句 重构更加困难——如果代码被移动了,那么导入语句就必须更新 减少混乱,整理文件内容——这里的目标是“消除干扰”。

These arguments were more convincing before IDEs did all of that automatically. If you're using a plain text editor instead of an IDE, then these arguments have some merit. But if you're using a plain text editor, you are already subjecting yourself to a number of other much more significant inefficiencies, and managing import statements is just one among many things that you should stop doing by hand. IDEs offer automatic management of imports, powerful refactoring tools, and folding (hiding) of any parts of the code you don't want to see.

对于“避免通配符”的支持者,有很多例子,但我只指出一个:

清晰度——特别是当有新人进入代码库时。他们将带着问题来到这里,并在探索代码的过程中继续发现新的问题。对于这个新的代码贡献者,通配符导入语句不能回答任何问题,在最坏的情况下可能会产生混乱、误解和新问题。相反,对于显式导入(并使用IDE),最坏的情况是中性的:没有提供新的信息;最好的情况是,它不仅可以减少歧义,而且还可以提供答案。

在一天结束的时候,它帮助整个团队降低(尽管是以一种很小的方式)代码的复杂性,减少混乱,增加清晰度。

在DDD书中

在实现将基于的任何开发技术中,寻找最小化的方法 重构模块的工作。在Java中,无法逃避导入到单个类中,只能逃避导入到您 一次至少可以导入整个包,以反映包是高度内聚单元的意图吗 同时减少了更改包名的工作量。

如果它弄乱了本地命名空间,那不是你的错——是包的大小造成的。

在Java import语句中使用通配符并不坏。

在《Clean Code》中,Robert C. Martin建议使用它们来避免冗长的导入列表。

以下是建议:

J1: Avoid Long Import Lists by Using Wildcards If you use two or more classes from a package, then import the whole package with import package.*; Long lists of imports are daunting to the reader. We don’t want to clutter up the tops of our modules with 80 lines of imports. Rather we want the imports to be a concise statement about which packages we collaborate with. Specific imports are hard dependencies, whereas wildcard imports are not. If you specifically import a class, then that class must exist. But if you import a package with a wildcard, no particular classes need to exist. The import statement simply adds the package to the search path when hunting for names. So no true dependency is created by such imports, and they therefore serve to keep our modules less coupled. There are times when the long list of specific imports can be useful. For example, if you are dealing with legacy code and you want to find out what classes you need to build mocks and stubs for, you can walk down the list of specific imports to find out the true qualified names of all those classes and then put the appropriate stubs in place. However, this use for specific imports is very rare. Furthermore, most modern IDEs will allow you to convert the wildcarded imports to a list of specific imports with a single command. So even in the legacy case it’s better to import wildcards. Wildcard imports can sometimes cause name conflicts and ambiguities. Two classes with the same name, but in different packages, will need to be specifically imported, or at least specifically qualified when used. This can be a nuisance but is rare enough that using wildcard imports is still generally better than specific imports.

我更喜欢特定的导入,因为它允许我查看文件中使用的所有外部引用,而无需查看整个文件。(是的,我知道不一定会有完全合格的推荐信。但我尽量避免使用。)