在 system.out 上,需要澄清
我正在查看某人的代码,看到他反复声明
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这是一个合理的做法。他基本上是在为
System.out
创建一个别名。有许多优点:This is a reasonable approach. He is basically creating an alias for
System.out
. There are a number of advantages:为了避免在执行小型测试(没有 IDE )时特别输入 System.out.println ,我使用 import static java.lang.System.out 代替
但这可能是有意义的,如果您想稍后替换 System.out 的值,也许替换为包装器以重定向到文件并立
即使标准输出静音。我重复一遍可能在某些情况下有意义,因为为此我将使用日志框架。
顺便说一句,最好将其声明为最终的和静态的:
To avoid typing
System.out.println
specially when performing small test ( without an IDE ) I useimport static java.lang.System.out
insteadBut that may make sense if you want to substitute the value of
System.out
later, perhaps to a wrapper to redirect to a fileAnd 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:
这可能是因为通常不建议深入研究和使用属于其他对象的成员的对象。这看起来就像有人伸手从你的口袋里掏出你的钱,而不是要求你借给他一些钱。
这样做可能有一点好处,如果需要的话,可以将输出流更改为文件、套接字或其他任何内容。因此,他可以将:替换
为
但是,如果他一遍又一遍地重复声明,他实际上会失去上述优势,因为重点是将其集中在某个地方,并决定在一个地方输出日志。
请注意,这是一种非常粗略的方法,真正的标准做法是使用日志记录。 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:
with
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.
如果您执行大量
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 thinkSystem.out.println
is clearer since you don't need to figure out whereout
was assigned. I setup an eclipse template so I can autocompleteprintln
toSystem.out.println
and it's pretty quick.没有。以前从未见过。
我同意。也不算太破烂吧...
Nope. Never seen that before.
I agree. It's not too shabby...
如果您查看文档中的 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.
这是一个可爱的把戏,但像大多数可爱的把戏一样,正确地做可能会更好。
一旦您确定需要日志记录(肯定要添加类似的内容),为什么不直接使用 Log4J 或其他具有更大灵活性和功能的日志系统呢?
如果你独自一人,别名是一种可爱又有趣的东西,但即使你是一个非常慢的打字员,每次你打字它都会为你节省整整 5 秒(通过 System.out 或
"sysout +"
in eclipse/netbeans),当某人(可能是你)第一次看到“out”时,你将损失十倍的时间。并且不立即知道这意味着什么。我的意思是,在一个程序中,假设您按照其他发帖者的建议进行了操作,并将“out”重定向到文件而不是 STDOUT,但在某些课程中,也许“out”仍然会转到 System.out。或者也许您只是忘记将其重定向到文件。后来你进来并说“好吧,它说:
但我在 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:
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.
这是否是一个好主意还有待商榷,并且可能取决于具体情况。
从好的方面来说:
不利的一面是:
out
变量是实例变量或者必须作为参数传递。应该指出的是,还有其他可能的方法可以做到这一点;例如,用实用方法
printLine(String)
替换System.out.println(String)
可以实现类似的效果,而无需交叉耦合。Whether this is a good idea is debatable, and probably depends on the circumstances.
On the plus side:
On the minus side:
out
variable is an instance variable or has to be passed as a parameter.System.setOut()
.It should be noted that there are potentially other ways to do this; e.g. replacing
System.out.println(String)
with a utility methodprintLine(String)
achieves a similar effect without the cross coupling.由于
System.out
是一个final
变量,因此他所做的与直接引用System.out
相同。除非有人调用
System.setOut()
重新分配System.out
。等等,什么?
since
System.out
is afinal
variable, what he did would be identitcal to referencingSystem.out
directly.unless somebody called
System.setOut()
which reassignedSystem.out
.wait, what?