使用单个语句更方便,更简洁,比如
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.sql.Date vs . java.util.Date)很容易通过其他方法来补救,而不是通过特定的导入来真正解决,当然也不能证明对所有类进行疯狂的迂腐的导入是正确的。没有什么比打开一个源文件并不得不翻看100条import语句更令人不安的了。
执行特定的导入会使重构更加困难;如果删除/重命名一个类,则需要删除其所有特定的导入。如果您将一个实现切换到同一个包中的不同类,则必须修复导入。虽然这些额外的步骤是可以自动化的,但它们实际上是对生产力的打击,没有真正的收益。
如果Eclipse在默认情况下不进行特定的类导入,那么每个人仍然会进行星型导入。我很抱歉,但是做特定的导入确实没有合理的理由。
下面是处理阶级冲突的方法:
import java.sql.*;
import java.util.*;
import java.sql.Date;
其他回答
在以前的项目中,我发现将*-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; }
我更喜欢特定的导入,因为它允许我查看文件中使用的所有外部引用,而无需查看整个文件。(是的,我知道不一定会有完全合格的推荐信。但我尽量避免使用。)
There is no runtime impact, as compiler automatically replaces the * with concrete class names. If you decompile the .class file, you would never see import ...*. C# always uses * (implicitly) as you can only using package name. You can never specify the class name at all. Java introduces the feature after c#. (Java is so tricky in many aspects but it's beyond this topic). In Intellij Idea when you do "organize imports", it automatically replaces multiple imports of the same package with *. This is a mandantory feature as you can not turn it off (though you can increase the threshold). The case listed by the accepted reply is not valid. Without * you still got the same issue. You need specify the pakcage name in your code no matter you use * or not.
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也会为您这样做)。