import java.util.*; 和 import java.util.* 有什么区别?并导入 java.util.Date; ?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1649430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between import java.util.*; and import java.util.Date; ?
提问by EthanZ6174
I just want to output current and I wrote
我只想输出电流,我写了
import java.util.*;
at beginning, and
一开始,和
System.out.println(new Date());
in the main part.
在主要部分。
But what I got was something like this:
但我得到的是这样的:
Date@124bbbf
When I change the import to import java.util.Date;
the code works perfectly, why?
当我将导入更改为import java.util.Date;
代码工作正常时,为什么?
====================================
====================================
The problem was, OK, my source file was "Date.java", that's the cause.
问题是,好吧,我的源文件是“Date.java”,这就是原因。
Well, it is all my fault, I confused everybody around ;P
好吧,这都是我的错,我把周围的人都弄糊涂了;P
And thanks to everyone below. It's really NICE OF YOU ;)
并感谢下面的每一个人。你真的很好 ;)
采纳答案by sleske
You probably have some other "Date" class imported somewhere (or you have a Date class in you package, which does not need to be imported). With "import java.util.*" you are using the "other" Date. In this case it's best to explicitly specify java.util.Date in the code.
您可能在某处导入了其他一些“Date”类(或者您的包中有一个 Date 类,不需要导入)。使用“import java.util.*”,您使用的是“其他”日期。在这种情况下,最好在代码中明确指定 java.util.Date。
Or better, try to avoid naming your classes "Date".
或者更好的是,尽量避免将您的类命名为“日期”。
回答by cherouvim
Your program should work exactly the same with either import java.util.*;or import java.util.Date;. There has to be something else you did in between.
您的程序应该与import java.util.*;完全相同。或导入 java.util.Date; . 你必须在这两者之间做了一些其他的事情。
回答by Chris R
import java.util.*;
imports everything within java.util including the Date class.
导入 java.util 中的所有内容,包括 Date 类。
import java.util.Date;
just imports the Date class.
只导入 Date 类。
Doing either of these could not make any difference.
执行其中任何一个都没有任何区别。
回答by Vijay Dev
but what I got is something like this: Date@124bbbf
while I change the import to: import java.util.Date;
the code works perfectly, why?
What do you mean by "works perfectly"? The output of printing a Date object is the same no matter whether you imported java.util.* or java.util.Date. The output that you get when printing objects is the representation of the object by the toString() method of the corresponding class.
“完美运行”是什么意思?无论您导入 java.util.* 还是 java.util.Date,打印 Date 对象的输出都是相同的。打印对象时得到的输出是相应类的 toString() 方法对对象的表示。
回答by Fedearne
The toString()
implementation of java.util.Date
does not depend on the way the class is imported. It always returns a nice formatted date.
的toString()
实现java.util.Date
不依赖于类的导入方式。它总是返回一个很好的格式化日期。
The toString()
you see comes from another class.
在toString()
你看到来自另一个类。
Specific import have precedence over wildcard imports.
特定导入优先于通配符导入。
in this case
在这种情况下
import other.Date
import java.util.*
new Date();
refers to other.Date
and not java.util.Date
.
指other.Date
而不是java.util.Date
。
The odd thing is that
奇怪的是
import other.*
import java.util.*
Should give you a compiler error stating that the reference to Date is ambiguous because both other.Date
and java.util.Date
matches.
如果给你一个编译器错误,指出参考日期是模糊的,因为这两个other.Date
和java.util.Date
匹配。