【发布时间】:2020-11-04 21:16:26
【问题描述】:
我在 java 中遇到了一个我无法解决的问题,这里是: 创建一个从 java.io.PrintStream 派生的名为 WebPrintStream 的类。 该类必须使用以下构造函数: WebPrintStream(OutputStream 输出,布尔刷新)
传入参数的值必须提供给 父类的构造函数。
创建 printbr () 方法,该方法接受一个值作为参数,将其转换为 String(如果需要)并在 OutputStream 通过附加字符串和换行符。为此,您可以使用其父类的 println () 和/或 print ()。 (换句话说,你不需要工作 直接在 OutputStream 对象上)。
与父类 println () 和 print () 的方法一样,您的 printbr () 方法必须支持以下类型传入参数的值:String、boolean、long、char、 char []、float、double、object、int。 这是要完成的代码:
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 () 方法必须支持以下类型的参数传入的值:String、boolean、long、字符、 char []、float、double、object、int。” p>
请问你有解决办法吗?
【问题讨论】:
-
嗨,也许用
public class WebPrintStream extends PrintStream扩展类,并用@Override注释覆盖的方法 -
是的,我做到了,现在覆盖方法的主体呢?我应该如何完成它们?
标签: java