(JAVA)装饰流

package IODemo;

/**
 * @author Alina
 * @date 2021年11月15日 11:48 下午
 * 设计思想:设计模式,装饰模式
 *      JAVA中有23种设计思想,全部基于面向对象
 *      装饰设计模式,核心思想,解决什么问题
 *      增强原有对象的功能
 *
 */
//第一代人类只有吃饭的功能
class Person{
    public  void eat(){
        System.out.println("吃饱饭");
    }
}
//第二代人类有了新的吃法
class newPerson{
    private Person per;
    //提供构造函数
    public newPerson(Person pe) {
        this.per = pe;
    }
    public  void neweat(){
        System.out.println("喝奶茶");
        per.eat();
        System.out.println("吃巧克力");
    }
}

public class buffer {
    public static void main(String[] args) {
        Person p = new Person();
        newPerson n = new newPerson(p);
        n.neweat();
    }

package IODemo;

import java.io.*;

/**
 * @author Alina
 * @date 2021年11月16日 11:35 下午
 * 实现读取文本一行
 * 读取一行返回字符串
 * 文件默认读到行位返回-1
 *
 */
class myread{
    private Reader reader;

    public myread(Reader reader) {
        this.reader = reader;
    }
    public String readLine()throws IOException {
        //创建字符串缓冲区对象,保留读取到的有效字符
        StringBuilder builder = new StringBuilder();
        int len = 0 ;
        //利用Reader类的方法read读取文件,read 文件结尾返回-1
        while ((len =reader.read())!= -1){
            //判断读取到的是不是\r
            if (len == '\r'){ continue;}
            //判断读取到的是否是\n
            if(len == '\n'){return builder.toString();}
            //如果读取的不是\r\n 视为有效字符,存放到缓冲区中
            builder.append((char) len);
        }
        //判断缓冲区是否有字符,如果有返回去
        if(builder.length()!=0){return builder.toString();}
        //文件读取完毕
        return null;
    }
    //定义关闭资源方法
    public void close()throws IOException{
        reader.close();
    }
}
public class myReadLine {
    public static void main(String[] args)throws IOException {
        myread  my = new myread(new FileReader("/src/IODemo/test.txt"));
        String line = null;
        while ((line = my.readLine())!=null){
            System.out.println(line);
        }

       // BufferedReader br = new BufferedReader(new FileReader("/src/IODemo/test.txt"));

    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值