使用单个语句更方便,更简洁,比如
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语句中使用通配符有什么问题?
当前回答
最重要的是导入java.awt。*可以使您的程序与未来的Java版本不兼容:
假设你有一个名为“ABC”的类,你使用的是JDK 8,你导入java.util.*。现在,假设Java 9出来了,它在包Java中有一个新类。直到它碰巧也被称为“ABC”。您的程序现在不能在Java 9上编译,因为编译器不知道您的“ABC”名称是指您自己的类还是Java .awt中的新类。
如果只从java显式地导入这些类,就不会遇到这个问题。你实际使用的。
资源:
Java进口
其他回答
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.
在DDD书中
在实现将基于的任何开发技术中,寻找最小化的方法 重构模块的工作。在Java中,无法逃避导入到单个类中,只能逃避导入到您 一次至少可以导入整个包,以反映包是高度内聚单元的意图吗 同时减少了更改包名的工作量。
如果它弄乱了本地命名空间,那不是你的错——是包的大小造成的。
郑重声明: 当您添加导入时,您也在指示您的依赖项。
您可以很快看到文件的依赖关系(不包括相同名称空间的类)。
最重要的是导入java.awt。*可以使您的程序与未来的Java版本不兼容:
假设你有一个名为“ABC”的类,你使用的是JDK 8,你导入java.util.*。现在,假设Java 9出来了,它在包Java中有一个新类。直到它碰巧也被称为“ABC”。您的程序现在不能在Java 9上编译,因为编译器不知道您的“ABC”名称是指您自己的类还是Java .awt中的新类。
如果只从java显式地导入这些类,就不会遇到这个问题。你实际使用的。
资源:
Java进口