Java out.println() 这怎么可能?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2504078/
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
Java out.println() how is this possible?
提问by user69514
I've seen some code such as:
我见过一些代码,例如:
out.println("print something");
I tried import java.lang.System;
我试过 import java.lang.System;
but it's not working. How do you use out.println()
?
但它不起作用。你怎么用out.println()
?
采纳答案by sfussenegger
static imports do the trick:
静态导入可以解决问题:
import static java.lang.System.out;
or alternatively import every static method and field using
或者使用导入每个静态方法和字段
import static java.lang.System.*;
Addendum by @Steve C: note that @sfussenegger said this in a comment on my Answer.
@Steve C 的附录:请注意,@sfussenegger 在对我的回答的评论中说过这一点。
"Using such a static import of System.out isn't suited for more than simple run-once code."
“使用 System.out 的这种静态导入不仅仅适用于简单的一次性代码。”
So please don't imagine that he (or I) think that this solution is Good Practice.
所以请不要想象他(或我)认为这个解决方案是好的做法。
回答by tangens
PrintStream out = System.out;
out.println( "hello" );
回答by Oskar Kjellin
You will have to create an object outfirst. More about this here:
您必须创建一个对象了第一。更多关于这里的信息:
// write to stdout
out = System.out;
out.println("Test 1");
out.close();
回答by Casey
Well, you would typically use
嗯,你通常会使用
System.out.println("print something");
which doesn't require any imports. However, since out is a static field inside of System, you could write use a static import like this:
这不需要任何进口。但是,由于 out 是 System 内部的静态字段,您可以编写使用静态导入,如下所示:
import static java.lang.System.*;
class Test {
public static void main(String[] args) {
out.println("print something");
}
}
Take a look at this link. Typically you would only do this if you are using a lot of static methods from a particular class, like I use it all the time for junit asserts, and easymock.
看看这个链接。通常,只有在使用特定类中的大量静态方法时才会这样做,就像我一直将它用于 junit 断言和 easymock。
回答by Chris Knight
Or simply:
或者干脆:
System.out.println("Some text");
回答by Stephen C
@sfussenegger's answer explains how to make this work. But I'd say don't do it!
@sfussenegger 的回答解释了如何进行这项工作。但我会说不要这样做!
Experienced Java programmers use, and expect to see
有经验的 Java 程序员使用,并希望看到
System.out.println(...);
and not
并不是
out.println(...);
A static import of System.out or System.err is (IMO) bad style because:
System.out 或 System.err 的静态导入是(IMO)糟糕的风格,因为:
- it breaks the accepted idiom, and
- it makes it harder to track down unwanted trace prints that were added during testing and not removed.
- 它打破了公认的习语,并且
- 这使得追踪在测试期间添加但未删除的不需要的痕迹打印变得更加困难。
If you find yourself doing lots of output to System.out or System.err, I think it is a better to abstract the streams into attributes, local variables or methods. This will make your application more reusable.
如果您发现自己对 System.out 或 System.err 进行了大量输出,我认为将流抽象为属性、局部变量或方法会更好。这将使您的应用程序更具可重用性。
回答by Exorcismus
you can see this also in sockets ...
您也可以在套接字中看到这一点...
PrintWriter out = new PrintWriter(socket.getOutputStream());
out.println("hello");
回答by Dushyant Thakur
out
is a PrintStream
type of static variable(object) of System
class and println()
is function of the PrintStream
class.
out
是PrintStream
类的一种静态变量(对象),是System
类println()
的函数PrintStream
。
class PrintStream
{
public void println(){} //member function
...
}
class System
{
public static final PrintStream out; //data member
...
}
That is why the static variable(object) out
is accessed with the class name System
which further invokes the method println()
of it's type PrintStream
(which is a class).
这就是为什么out
使用类名访问静态变量(对象)的原因,该类名System
进一步调用println()
其类型PrintStream
(这是一个类)的方法。
回答by Dushyant Thakur
simply import :
只需导入:
import static java.lang.System.*;