【问题标题】:Create a class that extends the java.io.PrintStream class创建一个扩展 java.io.PrintStream 类的类
【发布时间】:2020-11-04 21:16:26
【问题描述】:

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

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

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

与父类 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 ---------- * /

Voici le code que j'ai proposé pour la classe WebPrintStream

import java.io.PrintStream;

public class WebPrintStream extends PrintStream {

     @override
    public WebPrintStream(PrintStream out, boolean flush) {
        
    }
     @override
    public void printbr(String string) {
        
        
    }

}

这是我为 WebPrintStream 类提出的代码,但我知道它根本不完整,对于 printbr () 方法,我不知道如何用 java 表达这句话code: "你的 printbr () 方法必须支持以下类型的参数传入的值:Stringbooleanlong、字符char []floatdoubleobjectint。” p>

请问你有解决办法吗?

【问题讨论】:

  • 嗨,也许用public class WebPrintStream extends PrintStream 扩展类,并用@Override 注释覆盖的方法
  • 是的,我做到了,现在覆盖方法的主体呢?我应该如何完成它们?

标签: java


【解决方案1】:

试试这个:

   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");
    }
}

【讨论】:

  • 好吧,为什么是 super(out,autoFlush)?而不是 super(out,flush) 什么是 autoFlush?
  • 错误:public WebPrintStream(OutputStream out, boolean flush) { super(out, flush); }
  • 是的,它可以工作,printbr() 方法的主体怎么样?我该如何编写该主体?那么 WebPrintStream() 方法的主体呢?我该如何编写它呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-15
  • 1970-01-01
  • 1970-01-01
  • 2015-06-08
  • 2013-04-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多