使用单个语句更方便,更简洁,比如
import java.awt.*;
而不是导入一堆单独的类
import java.awt.Panel;
import java.awt.Graphics;
import java.awt.Canvas;
...
在import语句中使用通配符有什么问题?
使用单个语句更方便,更简洁,比如
import java.awt.*;
而不是导入一堆单独的类
import java.awt.Panel;
import java.awt.Graphics;
import java.awt.Canvas;
...
在import语句中使用通配符有什么问题?
当前回答
它使您的名称空间变得混乱,要求您完全指定任何有歧义的类名。最常见的情况是:
import java.util.*;
import java.awt.*;
...
List blah; // Ambiguous, needs to be qualified.
它还有助于使依赖项具体化,因为所有依赖项都列在文件的顶部。
其他回答
在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.
在以前的项目中,我发现将*-imports更改为特定的导入可以将编译时间减少一半(从大约10分钟减少到大约5分钟)。*-import使编译器搜索列出的每个包,以查找与您使用的类相匹配的类。虽然这段时间可能很小,但对于大项目来说,它会累积起来。
*-import的一个副作用是开发人员会复制和粘贴常用的导入行,而不是考虑他们需要什么。
以下是我关于这个话题的一些发现。
During compilation, the compiler tries to find classes that are used in the code from the .* import and the corresponding byte code will be generated by selecting the used classes from .* import. So the byte code of using .* import or .class names import will be same and the runtime performance will also be the same because of the same byte code. In each compilation, the compiler has to scan all the classes of .* package to match the classes that are actually used in the code. So, code with .* import takes more time during the compilation process as compared to using .class name imports. Using .* import helps to make code more cleaner Using .* import can create ambiguity when we use two classes of the same name from two different packages. Eg, Date is available in both packages. import java.util.*; import java.sql.*; public class DateDemo { private Date utilDate; private Date sqlDate; }
唯一的问题是它会混淆本地名称空间。例如,假设您正在编写一个Swing应用程序,因此需要java.awt。事件,并且还与公司的日历系统进行接口,该系统具有com.mycompany.calendar.Event。如果你使用通配符方法导入两者,会发生以下三种情况之一:
event和com.mycompany.calendar之间存在完全的命名冲突。事件,因此您甚至无法编译。 您实际上只导入了一个(两个导入中只有一个导入了。*),但它是错误的,并且您很难弄清楚为什么代码声称类型是错误的。 在编译代码时,没有com.mycompany.calendar。事件,但当他们后来添加一个时,您以前有效的代码突然停止编译。
显式列出所有导入的好处是,我可以一眼看出您打算使用哪个类,这使得代码的阅读更加容易。如果您只是在做一个快速的一次性的事情,那么没有什么明显的错误,但是未来的维护者会因为您的清晰而感谢您。
我更喜欢特定的导入,因为它允许我查看文件中使用的所有外部引用,而无需查看整个文件。(是的,我知道不一定会有完全合格的推荐信。但我尽量避免使用。)