黑马程序员技术交流社区
标题:
装饰者模式
[打印本页]
作者:
shenlibing666
时间:
2016-8-19 18:19
标题:
装饰者模式
package cn.itcast.other;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
/*
* 编写一个类拓展BufferedReader的功能, 增强readLine方法返回 的字符串带有行号+ 分号。
*
* */
public class Demo1 {
public static void main(String[] args) throws IOException {
File file=new File("Demo1.java");
FileReader fileReader=new FileReader(file);
BufferedReader bufferedReader=new BufferedReader(fileReader);
BufferedHangHao bufferedHangHao=new BufferedHangHao(bufferedReader);
BufferedFengHao bufferedFengHao=new BufferedFengHao(bufferedHangHao);
String line=null;
while((line=bufferedFengHao.readLine())!=null){
System.out.println(line);
}
}
}
//行号类
class BufferedHangHao extends BufferedReader {
BufferedReader bufferedReader;
BufferedHangHao(BufferedReader bufferedReader) {
super(bufferedReader);
this.bufferedReader = bufferedReader;//疑问:如果没有这句话为什么会报错???
}
int count = 1;
public String readLine() throws IOException {
String line = bufferedReader.readLine();
if(line==null){
return null;
}
line = count + "" + line;
count++;
return line;
}
}
//分号类
class BufferedFengHao extends BufferedReader{
BufferedReader bufferedReader;
public BufferedFengHao(BufferedReader bufferedReader) {
super(bufferedReader);
this.bufferedReader = bufferedReader;
}
public String readLine() throws IOException {
String line=bufferedReader.readLine();
if(line==null){
return null;
}
line=line+";";
return line;
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2