在 system.out 上,需要澄清

发布于 2024-09-05 11:24:59 字数 182 浏览 9 评论 0原文

我正在查看某人的代码,看到他反复声明

PrintStream out = System.out;

并随后调用

out.println("blah");

,我实际上认为这有点简洁。这是常见做法吗?难道他只是一时兴起?

I was looking at someone's code and saw that he repeatedly declared

PrintStream out = System.out;

and later called

out.println("blah");

I actually thought this was kind of neat. Is this a common practice? Was he just being fancy?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

望喜 2024-09-12 11:24:59

这是一个合理的做法。他基本上是在为 System.out 创建一个别名。有许多优点:

  • 减少打字。
  • 以后更容易更改代码以输出到不同的 PrintStream。
  • 可能会提高性能,尽管可以忽略不计。

This is a reasonable approach. He is basically creating an alias for System.out. There are a number of advantages:

  • Less typing.
  • Easier to later change the code to output to a different PrintStream.
  • Possibly a performance improvement, although it will be negligible.
怼怹恏 2024-09-12 11:24:59

为了避免在执行小型测试(没有 IDE )时特别输入 System.out.println ,我使用 import static java.lang.System.out 代替

但这可能是有意义的,如果您想稍后替换 System.out 的值,也许替换为包装器以重定向到文件并立

 PrintStream out = new FilePrintStream("MyLogs.log"); // // System.out

即使标准输出静音。我重复一遍可能在某些情况下有意义,因为为此我将使用日志框架。

顺便说一句,最好将其声明为最终的和静态的:

class YourClass  {
    private final static PrintStream out = System.out;
}

To avoid typing System.out.println specially when performing small test ( without an IDE ) I use import static java.lang.System.out instead

But that may make sense if you want to substitute the value of System.out later, perhaps to a wrapper to redirect to a file

 PrintStream out = new FilePrintStream("MyLogs.log"); // // System.out

And silence the standard output at once. I repeat it may make sense on some scenarios, because for this I would use a Logging framework.

BTW, it would be better to declare it as final and static also:

class YourClass  {
    private final static PrintStream out = System.out;
}
〃安静 2024-09-12 11:24:59

这可能是因为通常不建议深入研究和使用属于其他对象的成员的对象。这看起来就像有人伸手从你的口袋里掏出你的钱,而不是要求你借给他一些钱。

这样做可能有一点好处,如果需要的话,可以将输出流更改为文件、套接字或其他任何内容。因此,他可以将:替换

PrintStream out = System.out;

PrintStream out = new PrintStream(new FileOutputStream(filename));

但是,如果他一遍又一遍地重复声明,他实际上会失去上述优势,因为重点是将其集中在某个地方,并决定在一个地方输出日志。

请注意,这是一种非常粗略的方法,真正的标准做法是使用日志记录。 Java 有自己的开箱即用的 java.util.logging 包,log4j 是另一个非常强大的替代方案(并且非常流行),还有其他包。

It might be because in general it is not recommended to delve in and use objects which are members of other objects. It is seen like someone reaching for your pocket to get your money out of your wallet instead of asking you to lend him some money.

There might be a slight advantage to this which would be to be able to change the Output stream if needed to a file, socket or whatever. So he would be able to replace:

PrintStream out = System.out;

with

PrintStream out = new PrintStream(new FileOutputStream(filename));

However if he is repeatedly declaring it over and over again he is really losing the above advantage, because the whole point would be to have it somewhere centralised and decide where to output the logs in one place.

Note that this is a very crude way and the real standard practice is to use logging. Java has its own package java.util.logging out of the box, log4j is another very powerful alternative (and very popular) and there are others.

浮云落日 2024-09-12 11:24:59

如果您执行大量 println,这是一个快捷方式。我之前在其他地方看到过这样做,但我倾向于不这样做,因为我认为 System.out.println code> 更清晰,因为您不需要弄清楚 out 被分配到哪里。我设置了一个 Eclipse 模板,这样我就可以将 println 自动完成为 System.out.println,而且速度非常快。

It's a shortcut if you're doing a lot of println. I've seen it done in places before, though I tend not to do it because I think System.out.println is clearer since you don't need to figure out where out was assigned. I setup an eclipse template so I can autocomplete println to System.out.println and it's pretty quick.

Saygoodbye 2024-09-12 11:24:59

没有。以前从未见过。

我同意。也不算太破烂吧...

Nope. Never seen that before.

I agree. It's not too shabby...

画▽骨i 2024-09-12 11:24:59

如果您查看文档中的 Throwable.printStackTrace,您可以在不带参数的情况下调用它,它只是将 System.out 传递到采用 PrintStream 的版本。

可以传递许多不同的 PrintStream 对象的情况很常见,这使得打印代码更加简单。

If you look at Throwable.printStackTrace in the documentation you can call it without arguments, and it just passes System.out to the version which takes a PrintStream.

It's quite common to find situations where a number of different PrintStream objects could be passed around and it makes printing code much simpler.

北渚 2024-09-12 11:24:59

这是一个可爱的把戏,但像大多数可爱的把戏一样,正确地做可能会更好。

一旦您确定需要日志记录(肯定要添加类似的内容),为什么不直接使用 Log4J 或其他具有更大灵活性和功能的日志系统呢?

如果你独自一人,别名是一种可爱又有趣的东西,但即使你是一个非常慢的打字员,每次你打字它都会为你节省整整 5 秒(通过 System.out 或 "sysout +" in eclipse/netbeans),当某人(可能是你)第一次看到“out”时,你将损失十倍的时间。并且不立即知道这意味着什么。

我的意思是,在一个程序中,假设您按照其他发帖者的建议进行了操作,并将“out”重定向到文件而不是 STDOUT,但在某些课程中,也许“out”仍然会转到 System.out。或者也许您只是忘记将其重定向到文件。后来你进来并说“好吧,它说:

out.println("WE MADE IT");

但我在 STDOUT 中没有看到那一行,到底是什么?”

然后你花了 4 个小时来跟踪错误指示,而不是修复错误。

It's a cute trick, but like most cute tricks it might be better just to do it right.

Once you get to the point where you are sure you want logging (sure enough to add something like that), why not just get Log4J or some other logging system that has even more flexibility and power?

Aliasing stuff is kind of cute and fun if you are alone, but even if you are a really slow typist and it saves you a whole 5 seconds every time you type it (over System.out or "sysout +<ctrl-space>" in eclipse/netbeans), you will lose that time ten-fold the first time someone--possibly you--sees "out." and doesn't know immediately what it means.

I mean, in one program let's say that you did what some other poster suggested and redirected "out" to go to a file instead of STDOUT but In some classes, maybe "out" still goes to System.out. or maybe you just forget that you redirected it to a file. Later you come in and say "well, it says:

out.println("WE MADE IT");

but I don't see that line in STDOUT, what the hell?"

Then you spend 4 hours tracking a bad indication instead of fixing the bug.

極樂鬼 2024-09-12 11:24:59

这是否是一个好主意还有待商榷,并且可能取决于具体情况。

从好的方面来说:

  • 它使应用程序代码看起来更整洁。
  • 它可以简化更改输出目的地的过程。

不利的一面是:

  • 它可能会增加交叉耦合;例如,如果 out 变量是实例变量或者必须作为参数传递。
  • 如果您的应用程序需要调用 System.setOut(),则可能会导致麻烦。
  • 如果 System.out 代码只是在调试,这会使其更难被注意到和删除。事实上,它可能会使报告此类事情的 PMD(等)代码质量检查无效。

应该指出的是,还有其他可能的方法可以做到这一点;例如,用实用方法 printLine(String) 替换 System.out.println(String) 可以实现类似的效果,而无需交叉耦合。

Whether this is a good idea is debatable, and probably depends on the circumstances.

On the plus side:

  • It makes the application code look neater.
  • It may simplify changing the destination for output.

On the minus side:

  • It potentially increases cross-coupling; e.g. if the out variable is an instance variable or has to be passed as a parameter.
  • It potentially causes trouble if your application needs to call System.setOut().
  • If the System.out code is just debugging, this makes it harder to notice and remove. Indeed, it probably nullifies PMD (etc) code quality checks that report this kind of thing.

It should be noted that there are potentially other ways to do this; e.g. replacing System.out.println(String) with a utility method printLine(String) achieves a similar effect without the cross coupling.

花间憩 2024-09-12 11:24:59

由于 System.out 是一个 final 变量,因此他所做的与直接引用 System.out 相同。

除非有人调用 System.setOut() 重新分配 System.out

等等,什么?

since System.out is a final variable, what he did would be identitcal to referencing System.out directly.

unless somebody called System.setOut() which reassigned System.out.

wait, what?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文