本帖最后由 毛恩奇 于 2013-7-6 11:24 编辑
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- class MyQuotBufferedReader extends BufferedReader{
- BufferedReader b;
- public MyQuotBufferedReader(BufferedReader b){
- super(b);
- this.b=b;
- }
- public String readLine() throws IOException{
- String line=super.readLine();????
- if(line==null){
- return null;
- }
- return "\""+line+"\"";//每一行加双引号
- }
- }
- class MyNumBufferedReader extends BufferedReader{
- int count=1;
- BufferedReader b;
- public MyNumBufferedReader(BufferedReader b){
- super(b);
- this.b=b;
- }
- public String readLine() throws IOException{
- String line=super.readLine();//用super无法打印出引号,只有数字????????????????????????
- if(line==null){
- return null;
- }
- return count+++line;//每一行前面标行号
- }
- }
- public class Demo5 {
- public static void main(String[] args) throws IOException {
- MyNumBufferedReader mqr=new MyNumBufferedReader(new MyQuotBufferedReader(new BufferedReader(new FileReader("d:\\111.txt"))));
- String line=null;
- while((line=mqr.readLine())!=null){
- System.out.println(line);
- }
- mqr.close();
- }
- }
复制代码 以上是装饰设计模式代码,为什么 MyNumBufferedReader继承BufferedReader后,复写的时候调用super.readLine()和b.readLine()效果不一样,用super的时候只有行号没有我要的引号,用b.readLine()时才有,用super和b调用有什么区别啊,构造函数那里this.b=b有点多余吧 直接调用父类的构造函数就行了啊!!!
|