简体   繁体   中英

How to write Java system.out 'helper'?

Java/programming newbie learning a little of it in school and I am wondering if there is a 'standard' or preferred way of 'wrapping' System.out.xxx to either overload some output functions or if nothing else simplify the code a little if anyone has any tips.

I assume this is common knowledge I just don't have, but I may not have been asking the question right as (little) searching turned up less results.

I was going to extend PrintStream into MyPrint and instantiate with super(System.out), but to be frank have no real reason for doing so other than it being my first idea. Thanks much for any help or arrows in the right direction.

There is no way to extend the System.out.println , but you can set a PrintStream to the System.out by the method System.setOut , or you can create a wrapper class like the example below to do something before the print.

public class SysoutWrapper {

    public static void println(Object text){
        //do whaterever you desire here
        System.out.println(text);
    } 

}

Usage:

  import static SysoutWrapper.println;
  ...

  println("some message to print");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM