定义抽象父类
Reader_
public abstract class Reader_ {
public void readFile(){}
public void readString(){}
}
定义两个子类作为节点流
FileReader_
public class FileReader_ extends Reader_{
public void readFile(){
System.out.println("读取文件");
}
}
StringReader_
public class StringReader_ extends Reader_{
public void readString(){
System.out.println("读取字符串");
}
}
定义一个子类作为处理流
BufferedReader_
public class BufferedReader_ extends Reader_{
private Reader_ reader_;
public BufferedReader_(Reader_ reader_) {
this.reader_ = reader_;
}
public void readFile(){
reader_.readFile();
}
public void readFiles(int num){
for (int i=0;i<num;i++){
reader_.readFile();
}
}
public void readStrings(int num){
for (int i=0;i<num;i++){
reader_.readString();
}
}
}
测试
public class Test_ {
public static void main(String[] args) {
BufferedReader_ bufferedReader_ = new BufferedReader_(new FileReader_());
bufferedReader_.readFiles(10);
BufferedReader_ bufferedReader2 = new BufferedReader_(new StringReader_());
bufferedReader2.readStrings(10);
}
}