请告诉我在哪里可以找到这个库,以便使用 System.out.println()的短表达式,以及我应该把这个库放在哪里。
System.out.println()
您可以使用日志库,而不是重新发明轮子。例如,日志4j将为不同的消息(如 info()、 warn()和 error())提供方法。
info()
warn()
error()
或者简单地创建一个自己的 println方法,然后调用它:
println
void println(Object line) { System.out.println(line); } println("Hello World");
IntelliJ IDEA and NetBeans:
you type sout then press TAB, and it types System.out.println() for you, with the cursor in the right place.
sout
日食:
键入 syso,然后按 CTRL + SPACE。
syso
其他
为您喜欢的文本编辑器/IDE 找到一个“代码片段”插件
import static java.lang.System.out; out.println("Hello World");
println("Hello, World!")
println "Hello, World!"
print "Hello, World!"
puts "Hello, World!"
(println "Hello, World!")
print('Hello, World!');
Java 是一种冗长的语言。
如果你才上课3天,这已经让你感到困扰,也许你最好学习一门不同的语言,比如 Scala:
scala> println("Hello World") Hello World
从宽泛的意义上讲,这可以算是使用“库”来实现较短的表达式;)
使用 log4j 或 JDK 日志记录,这样您就可以在类中创建一个静态日志记录器,并像下面这样调用它:
LOG.info("foo")
void p(String l){ System.out.println(l); }
最短的,去吧。
一些有趣的选择:
选择一
PrintStream p = System.out; p.println("hello");
选择二
PrintWriter p = new PrintWriter(System.out, true); p.println("Hello");
正如 Bakkal 解释的,对于键盘快捷方式,在 netbeans中,您可以进入 tools-> options-> Editor-> code template 并添加或编辑您自己的快捷方式。
netbeans
在 Eclipse中,它在模板上。
Eclipse
这或许是个小问题,但是:
import static System.out; public class Tester { public static void main(String[] args) { out.println("Hello!"); } }
... 产生了编译时间错误。我通过编辑第一行来纠正这个错误:
import static java.lang.System.out;
我对 蓝色的解决方案是在 Program Files (x86) BlueJ lib 英语模板 newclass 中编辑 New Class 模板“ stdclass.tmpl”,并添加以下方法:
public static <T> void p(T s) { System.out.println(s); }
或者另一个版本:
public static void p(Object s) { System.out.println(s); }
至于 Eclipse,我使用的是建议的快捷方式 syso + <Ctrl> + <Space>:)
syso + <Ctrl> + <Space>
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!"); } }
使用 System.out.println ()是不好的做法(最好使用日志框架)-> 您的代码库中不应该出现很多情况。使用另一种方法简单地缩短它似乎不是一个好的选择。
对于 智慧的想法型 sout,按 Tab。
对于 日食型 syso,按 Ctrl + Space。
在 Java 8中:
List<String> players = new ArrayList<>(); players.forEach(System.out::println);