Java:使用导入还是显式包/类名? [英] Java: Use import or explicit package / class name?
问题描述
我正在编写一个非常基本的应用程序,扩展了 Swing JFrame.明确引用之间有什么区别:
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 {
...
或事先导入类:
import javax.swing.JFrame;
public class LineTest extends JFrame {
...
您何时(以及为什么)会使用一种技术而不是另一种技术?
When (and why) would you use one technique over the other?
推荐答案
没有真正的区别;生成的字节码将完全相同.import
语句实际上只是告诉编译器当我编写 JFrame
时,我实际上是指 javax.swing.JFrame
"的一种方式.
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
".
例如,如果您在不同的包中有两个同名的类,您可能想要使用完全限定的包名.标准库中的一个常见示例是 java.util.Date
和 java.sql.Date
类.
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屋!