Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建一个扩展java.io.PrintStream类的类_Java - Fatal编程技术网

创建一个扩展java.io.PrintStream类的类

创建一个扩展java.io.PrintStream类的类,java,Java,我在java中遇到了一个无法解决的问题,如下所示: 创建一个名为WebPrintStream的类,该类从java.io.PrintStream派生而来。 该类必须使用以下构造函数: WebPrintStream(OutputStream out,布尔刷新) 传入参数的值必须提供给 父类的构造函数 创建printbr()方法,该方法接受一个值作为参数,将其转换为字符串(如有必要),并通过附加字符串和换行符在输出流中显示其内容。为此,可以使用其父类的println()和/或print()。(换句话说

我在java中遇到了一个无法解决的问题,如下所示: 创建一个名为WebPrintStream的类,该类从java.io.PrintStream派生而来。 该类必须使用以下构造函数: WebPrintStream(OutputStream out,布尔刷新)

传入参数的值必须提供给 父类的构造函数

创建printbr()方法,该方法接受一个值作为参数,将其转换为字符串(如有必要),并通过附加字符串和换行符在输出流中显示其内容。为此,可以使用其父类的println()和/或print()。(换句话说,你不需要工作 直接在输出流对象上)

与父类println()print()的方法一样,printbr()方法必须支持传入参数的值的以下类型:Stringbooleanlongcharchar[]floatdoubleobjectint。 以下是要完成的代码:

public class ExerciseImpl {

    public void runExercise (String [] argv) throws Exception {
        // this variable will be filled at runtime
        WebPrintStream wp = new WebPrintStream (System.out, true);
        wp.printbr ("blah");

    }

}
/ *
 * Create a class named WebPrintStream which descends from java.io.PrintStream.
 * The class must work with the following constructor WebPrintStream (OutputStream out, boolean flush).
 * Create the printbr () method which accepts a value as a parameter , converts it to a String (if necessary)
 * and displays its contents in OutputStream by appending the string [it] <br/> [/ it] and a line break.
 * The method must support the following types for the value passed in parameter: String, boolean, long, char, char [], float, double, object, int
 * /

/ * ---------- DO NOT MODIFY THE CODE ABOVE THIS LINE, IT WILL BE RESET ON RUN ---------- * /

/ **** Enter your code here **** /

/ * ---------- DO NOT CHANGE THE CODE BELOW THIS LINE, IT WILL BE RESET WHEN RUNNING ---------- * /

这是我为WebPrintStream类建议的代码,但我知道它根本不完整,对于printbr()方法,我不知道如何用java代码表达这句话:“您的printbr()方法必须为传入参数的值支持以下类型:字符串布尔字符char[]floatdoubleobjectint

您有什么解决方案吗?

试试这个:

   import java.io.OutputStream;
import java.io.PrintStream;

public class WebPrintStream extends PrintStream {


    public WebPrintStream(OutputStream out, boolean autoFlush) {
        super(out, autoFlush);
    }

    public void printbr(Object obj) {

        this.print(obj);

    }

    public static void main(String[] args) {
        WebPrintStream webPrintStream = new WebPrintStream(System.out, true);
        webPrintStream.print("bah");
    }
}

您好,也许可以使用
公共类WebPrintStream扩展PrintStream
来扩展类,并使用
@Override
来注释被重写的方法是的,现在我做了,那么被重写的方法的主体呢?我应该如何完成它们?好的,为什么是超级(out,autoFlush)?而不是super(out,flush)什么是autoFlush?错误:public WebPrintStream(OutputStream out,boolean flush){super(out,flush);}是的,printbr()方法的主体如何工作?我如何编写该主体?然后WebPrintStream()方法的主体如何工作?我如何也编写它?
   import java.io.OutputStream;
import java.io.PrintStream;

public class WebPrintStream extends PrintStream {


    public WebPrintStream(OutputStream out, boolean autoFlush) {
        super(out, autoFlush);
    }

    public void printbr(Object obj) {

        this.print(obj);

    }

    public static void main(String[] args) {
        WebPrintStream webPrintStream = new WebPrintStream(System.out, true);
        webPrintStream.print("bah");
    }
}