首页 > 学院 > 开发设计 > 正文

处理流和装饰者模式

2019-11-14 21:49:46
字体:
来源:转载
供稿:网友
处理流和装饰者模式

处理流使用实例:BufferedReader 字符输入处理流

BufferedReader类里面有个方法叫readLine(),读取一行数据并返回这行数据

生成BufferedReader对象的方法:

BufferedReader br = new BufferedReader(new FileReader(“C:/”))

import java.io.*;class Test{public static void main(String args[]){FileReader fr = null;BufferedReader br =null;try{fr = new FileReader("F:/Android/Java4Android/34/src/users.txt");br = new BufferedReader(fr);String s = br.readLine();System.out.PRintln(s);}catch(Exception e){System.out.println(e);}finally{try{fr.close();br.close();}catch(Exception e){System.out.println(e);}}}}

装饰者模式举例:

interface Worker{public void doSomeWork();}
class Carpenter implements Worker{public void doSomeWork(){System.out.println("修门窗");}}
class Plumber implements Worker{public void doSomeWork(){System.out.println("修水管");}}
class Aworker implements Worker{private Worker worker;public Aworker(Worker aworker){this.worker = aworker;}public void doSomeWork(){System.out.println("你好");worker.doSomeWork();}}
class Test{public static void main(String args[]){Worker carpenter = new Carpenter();Aworker carpenterWorker = new Aworker(carpenter);carpenterWorker.doSomeWork();Worker plumber = new Plumber();Aworker plumberWorker = new Aworker(plumber);plumberWorker.doSomeWork();}}

  


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表