我曾经编写过C++或C程序。 在C中,我使用了
printf();
在C++中使用完
std:cout
我可以直接使用
标签。
cout<< variable;
我想知道是否有一种简单的方法可以打印 Java,而不需要每次都输入 system.out.print()?
附注:我知道我可以自己实现一个方法。只是想知道 Java 是否有这个功能,但我可能错过了它。
不。有几个选择:
As you say, just implement it in your class.
void println(String msg) { // String or Object or whatever
System.out.println(msg);
}
Use a variable in your method:
PrintStream a = System.out;
// ...
a.println("Hi there");
In his comment, Marko said you can import static System.out;
and then just use out.println
in your code.
syso
然后按下ctrl + space
,它会建议使用System.out.println(...)
。public static void println(String msg){
System.out.println(msg);
}
log4j
这样的东西,它可以在不同的日志级别上为您完成许多事情,以更简洁的方法名来完成此操作。大多数IDE都带有System.out.println
的代码模板,因此如果只是输入,这提供了一种解决方案。
然而,如果您主要使用System.out.println
来添加调试输出,那么您可以通过像这样的辅助方法打败您列出的任何内置习语的便利性:
public static <T> T spy(String msg, T x) { System.out.println(msg+": "+x); return x; }
优点是您可以将其插入到任何表达式的中间:
String result = dbCall(arg1, spy("Second arg", arg2), arg3);
我假设在上面使用了 spy
的静态导入。
System.out.print
对我来说很容易。 - BackSlashprintf
容易使用而System.out.println
不容易使用? - Marko Topolnikvoid x(String s) {System.out.println(s);}
。然后只需使用x("你好," + name);
。 - paxdiablosysout
。你也可以使用import static System.out
,尽管我从未感到有必要这样做。 - Marko Topolnik