Java 如何使 System.out.println() 更短
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3320764/
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
How to make System.out.println() shorter
提问by Eugene
Please advice on where can I find the lib in order to use the shorter expression of System.out.println()
and where should I place that lib.
请建议我在哪里可以找到 lib 以便使用更短的表达式System.out.println()
以及我应该在哪里放置该 lib。
采纳答案by bakkal
Logging libraries
日志库
You could use logging libraries instead of re-inventing the wheel. Log4jfor instance will provide methods for different messages like info()
, warn()
and error()
.
您可以使用日志库而不是重新发明轮子。例如,Log4j将为不同的消息提供方法,例如info()
,warn()
和error()
。
Homemade methods
自制方法
or simply make a println
method of your own and call it:
或者简单地创建一个println
自己的方法并调用它:
void println(Object line) {
System.out.println(line);
}
println("Hello World");
IDE keyboard shortcuts
IDE 键盘快捷键
IntelliJ IDEA and NetBeans:
IntelliJ IDEA 和 NetBeans:
you type sout
then press TAB, and it types System.out.println()
for you, with the cursor in the right place.
你输入sout
然后按TAB,它会System.out.println()
为你输入,光标在正确的位置。
Eclipse:
蚀:
Type syso
then press CTRL+ SPACE.
输入syso
然后按CTRL+ SPACE。
Other
其他
Find a "snippets" plugin for your favorite text editor/IDE
为您最喜欢的文本编辑器/IDE 找到一个“片段”插件
Static Import
静态导入
import static java.lang.System.out;
out.println("Hello World");
Explore JVM languages
探索 JVM 语言
Scala
斯卡拉
println("Hello, World!")
Groovy
常规
println "Hello, World!"
Jython
Jython
print "Hello, World!"
JRuby
红宝石
puts "Hello, World!"
Clojure
Clojure
(println "Hello, World!")
Rhino
犀牛
print('Hello, World!');
回答by dbyrne
Java is a verbose language.
Java 是一种冗长的语言。
If you are only 3 days in, and this is already bothering you, maybe you'd be better off learning a different language like Scala:
如果你才 3 天,这已经困扰你了,也许你最好学习一种不同的语言,比如 Scala:
scala> println("Hello World")
Hello World
In a loose sense, this would qualify as using a "library" to enable shorter expressions ;)
从广义上讲,这将有资格使用“库”来启用更短的表达式;)
回答by Jon Bringhurst
Use log4j or JDK logging so you can just create a static logger in the class and call it like this:
使用 log4j 或 JDK 日志记录,这样您就可以在类中创建一个静态记录器并像这样调用它:
LOG.info("foo")
回答by rup
void p(String l){
System.out.println(l);
}
Shortest. Go for it.
最短。去吧。
回答by Stephen Paul
Some interesting alternatives:
一些有趣的选择:
OPTION 1
选项1
PrintStream p = System.out;
p.println("hello");
OPTION 2
选项 2
PrintWriter p = new PrintWriter(System.out, true);
p.println("Hello");
回答by dazito
As Bakkal explained, for the keyboard shortcuts, in netbeans
you can go to tools->options->editor->code templates and add or edit your own shortcuts.
正如 Bakkal 解释的那样,对于键盘快捷键,netbeans
您可以转到工具->选项->编辑器->代码模板并添加或编辑您自己的快捷键。
In Eclipse
it's on templates.
在Eclipse
它的模板上。
回答by CaitlinG
A minor point perhaps, but:
也许是一个小问题,但是:
import static System.out;
public class Tester
{
public static void main(String[] args)
{
out.println("Hello!");
}
}
...generated a compile time error. I corrected the error by editing the first line to read:
...生成了编译时错误。我通过编辑第一行来纠正错误:
import static java.lang.System.out;
回答by carlos_lm
My solution for BlueJis to edit the New Class template "stdclass.tmpl" in Program Files (x86)\BlueJ\lib\english\templates\newclass and add this method:
我对BlueJ 的解决方案是编辑 Program Files (x86)\BlueJ\lib\english\templates\newclass 中的新类模板“stdclass.tmpl”并添加此方法:
public static <T> void p(T s)
{
System.out.println(s);
}
Or this other version:
或者这个其他版本:
public static void p(Object s)
{
System.out.println(s);
}
As for Eclipse I'm using the suggested shortcut syso + <Ctrl> + <Space>
:)
至于 Eclipse,我正在使用建议的快捷方式syso + <Ctrl> + <Space>
:)
回答by Denis
package some.useful.methods; public class B { public static void p(Object s){ System.out.println(s); } }
package first.java.lesson; import static some.useful.methods.B.*; public class A { public static void main(String[] args) { p("Hello!"); } }
回答by Marc T
Using System.out.println() is bad practice (better use logging framework) -> you should not have many occurences in your code base. Using another method to simply shorten it does not seem a good option.
使用 System.out.println() 是不好的做法(最好使用日志框架)-> 你的代码库中不应该有太多的出现。使用另一种方法来简单地缩短它似乎不是一个好的选择。