我有一个代码:
public class App {
public static void main(String[] args) {
System.out.println("Hello World from main!");
JShell shell = JShell.builder().build();
shell.eval("System.out.println(\"Hello World from JShell!\");");
}
}
现在我希望我只能为 JShell 设置输出流,而不是普通代码。
我试过了:
shell.eval("System.setOut(new Printer());");
shell.eval("System.out.println(\"Hello World!\");");
但是不起作用!
我的打印机类:
class Printer extends printstream{
public Printer() {
super(System.out);
}
@Override
public void print(String s) {
super.print("Message: - " + s);
}
}