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

import java.awt.*;

而不是导入一堆单独的类

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

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


当前回答

It helps to identify classname conflicts: two classes in different packages that have the same name. This can be masked with the * import. It makes dependencies explicit, so that anyone who has to read your code later knows what you meant to import and what you didn't mean to import. It can make some compilation faster because the compiler doesn't have to search the whole package to identify depdencies, though this is usually not a huge deal with modern compilers. The inconvenient aspects of explicit imports are minimized with modern IDEs. Most IDEs allow you to collapse the import section so it's not in the way, automatically populate imports when needed, and automatically identify unused imports to help clean them up.

我工作过的大多数使用大量Java的地方都将显式导入作为编码标准的一部分。我有时仍然使用*来快速创建原型,然后在产品化代码时展开导入列表(一些ide也会为您这样做)。

其他回答

性能:由于字节码相同,对性能没有影响。 尽管这会导致一些编译开销。

编译:在我的个人机器上,编译一个空白类而不导入任何东西需要100毫秒,但导入java时是同一个类。*占用170毫秒。

在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.

为什么在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中,无法逃避导入到单个类中,只能逃避导入到您 一次至少可以导入整个包,以反映包是高度内聚单元的意图吗 同时减少了更改包名的工作量。

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

忘掉混乱的命名空间……想想那些不得不在GitHub、vi、notepad++或其他非ide文本编辑器中阅读和理解你的代码的可怜人吧。

这个人必须煞费苦心地查找每个通配符作用域中所有类和引用中来自一个通配符的每个标记……只是想搞清楚到底发生了什么。

如果你只是为编译器编写代码——而且你知道你在做什么——我相信通配符没有问题。

但是,如果其他人(包括未来的您)希望一次阅读就能快速理解某个特定的代码文件,那么显式引用会有很大帮助。