Java:使用导入或显式包/类名? [英] Java: Use import or explicit package / class name?
问题描述
我在写一个非常基本的应用程序,扩展Swing JFrame。显式引用之间的区别是什么?
public class LineTest extends javax.swing.JFrame {
.. 。
或预先导入类:
import javax.swing.JFrame;
public class LineTest extends JFrame {
...
没有什么真正的区别;生成的字节码将完全相同。 import
语句实际上只是告诉编译器当我写 JFrame
时,我的意思是 javax.swing.JFrame
。
您可能想使用完全限定的包名称,如果你有例如两个类不同包中的相同名称。标准库中的一个常见示例是类 java.util.Date
和 java.sql.Date
。
I'm writing a very basic app, extending the Swing JFrame. What are the differences between making an explicit reference:
public class LineTest extends javax.swing.JFrame {
...
or importing the class beforehand:
import javax.swing.JFrame;
public class LineTest extends JFrame {
...
When (and why) would you use one technique over the other?
There is no real difference; the generated byte code will be exactly the same. An import
statement is really just a way to tell the compiler "when I write JFrame
, I actually mean javax.swing.JFrame
".
You might want to use fully-qualified package names if you have for example two classes with the same name in different packages. One common example from the standard library are the classes java.util.Date
and java.sql.Date
.
这篇关于Java:使用导入或显式包/类名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!