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

import java.awt.*;

而不是导入一堆单独的类

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

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


当前回答

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

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

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

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

其他回答

郑重声明: 当您添加导入时,您也在指示您的依赖项。

您可以很快看到文件的依赖关系(不包括相同名称空间的类)。

在以前的项目中,我发现将*-imports更改为特定的导入可以将编译时间减少一半(从大约10分钟减少到大约5分钟)。*-import使编译器搜索列出的每个包,以查找与您使用的类相匹配的类。虽然这段时间可能很小,但对于大项目来说,它会累积起来。

*-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也会为您这样做)。

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

这里是对明星进口的投票。import语句用于导入包,而不是类。导入整个包要干净得多;这里指出的问题(例如java.sql.Date vs . java.util.Date)很容易通过其他方法来补救,而不是通过特定的导入来真正解决,当然也不能证明对所有类进行疯狂的迂腐的导入是正确的。没有什么比打开一个源文件并不得不翻看100条import语句更令人不安的了。

执行特定的导入会使重构更加困难;如果删除/重命名一个类,则需要删除其所有特定的导入。如果您将一个实现切换到同一个包中的不同类,则必须修复导入。虽然这些额外的步骤是可以自动化的,但它们实际上是对生产力的打击,没有真正的收益。

如果Eclipse在默认情况下不进行特定的类导入,那么每个人仍然会进行星型导入。我很抱歉,但是做特定的导入确实没有合理的理由。

下面是处理阶级冲突的方法:

import java.sql.*;
import java.util.*;
import java.sql.Date;