简体   繁体   English

Java out.println() 这怎么可能?

[英]Java out.println() how is this possible?

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()

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.所以请不要想象他(或我)认为这个解决方案是好的做法。

PrintStream out = System.out;
out.println( "hello" );

@sfussenegger's answer explains how to make this work. @sfussenegger 的回答解释了如何进行这项工作。 But I'd say don't do it !但我会说不要这样做

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.如果您发现自己对 System.out 或 System.err 进行了大量输出,我认为将流抽象为属性、局部变量或方法会更好。 This will make your application or library more maintainable and more reusable.这将使您的应用程序或库更易于维护和重用。

(Obviously, if your Java program is a once-off thing that you intend to throw away when you have completed the current task, then maintainability is not a concern. But the flip side is that "throw away" code is often NOT thrown away.) (显然,如果你的 Java 程序是一个一次性的东西,你打算在完成当前任务后扔掉,那么可维护性不是问题。但另一方面是“扔掉”的代码通常不会被扔掉.)

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。

out is a PrintStream type of static variable(object) of System class and println() is function of the PrintStream class. outSystem类的静态变量(对象)的PrintStream类型,而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).这就是为什么使用类名System访问静态变量(对象) out原因,该类名进一步调用了PrintStream类型(它是一个类)的println()方法。

You will have to create an object out first.你必须首先创建一个对象 More about this here:更多关于这里的信息:

    // write to stdout
    out = System.out;
    out.println("Test 1");
    out.close();

you can see this also in sockets ...您也可以在套接字中看到这一点...

PrintWriter out = new PrintWriter(socket.getOutputStream());

out.println("hello");

只需导入:

import static java.lang.System.*;

或者干脆:

System.out.println("Some text");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM