装饰模式的简单运用(无注释版)
额!大家好!我又来了!!因为才刚刚写完所以就没时间去写代码!!我比较懒!这波怪我!!大家就先看着吧!!!
所谓的装饰者模式就是:在同一个父类的情况下,其子类中有一个用父类的对象来接收的子类的对象,并调用子类的重写父类的方法!
可能我说的不咋好但是理论上就是这样的!
其中父类位于最上,多个继承父类的子类位于其下!
子类A与子类B之间有一个接受的成员对象(父类)!
在用的时候将父类和子类A和子类B都创建出来!
想怎么调用就看自己的思想!可以试一下!真的!
可能讲的不怎么清晰或有一些缺点!多包涵!
1 import java.io.BufferedReader; 2 import java.io.FileNotFoundException; 3 import java.io.FileReader; 4 import java.io.IOException; 5 import java.io.Reader; 6 7 class BufferedReaderLine extends BufferedReader{ 8 int count = 0; 9 //BufferedReader bufferedReader; 10 11 public BufferedReaderLine(Reader in) { 12 super(in); 13 } 14 15 @Override 16 public String readLine() throws IOException { 17 String str = super.readLine(); 18 if(str == null){ 19 return null; 20 } 21 count++; 22 str = count + "\t" + str; 23 return str; 24 } 25 26 } 27 class BufferedReaderFen extends BufferedReader{ 28 29 BufferedReader bufferedReader; 30 31 public BufferedReaderFen(BufferedReader in) { 32 super(in); 33 this.bufferedReader = in; 34 } 35 36 @Override 37 public String readLine() throws IOException { 38 String str = bufferedReader.readLine(); 39 if(str == null){ 40 return null; 41 } 42 43 str = str + ";"; 44 return str; 45 } 46 47 } 48 49 50 public class two { 51 52 /** 53 * @param args 54 */ 55 public static void main(String[] args) { 56 // TODO Auto-generated method stub 57 try { 58 FileReader read = new FileReader("C:\\Users\\wang\\Desktop\\Demo2.java"); 59 BufferedReader reader = new BufferedReader(read); 60 //BufferedReaderLine rederLine = new BufferedReaderLine(reader); 61 BufferedReaderFen rederFen = new BufferedReaderFen(reader); 62 String str = null; 63 try { 64 while((str = rederFen.readLine()) != null){ 65 System.out.println(str); 66 } 67 rederFen.close(); 68 } catch (IOException e) { 69 System.out.println("读取失败!"); 70 throw new RuntimeException(e); 71 } 72 } catch (FileNotFoundException e) { 73 System.out.println("读取失败!"); 74 throw new RuntimeException(e); 75 } 76 } 77 78 }