26:IO技术--装饰设计模式
2018-03-11 15:37
141 查看
package tset1;
/*
自定义一个类,其中包含一个功能和readLine一致的方法。来模拟BufferedReader;
*/
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader( “D:\java\Pro\demo.txt”);
MyBufferedReader mbf = new MyBufferedReader( fr);
String line = null;
while (( line = mbf.myReadLine()) != null) {
System. out.println( line);
}
mbf.myClose();
}
}
class MyBufferedReader {
private FileReader r;
当想要对已有的对象进行功能增强时,
可以定义类,将已有对象传入,基于已有的功能,并提供加强功能。
那么自定义的该类成为装饰类。
package tset1;
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException {
new SuperPerson(new Person()).superChifan();
}
}
class Person {
public void eat() {
System. out.println( “吃饭”);
}
}
class SuperPerson {
private Person p;
}
装饰和继承的区别:
装饰模式比继承灵活,避免继承体系的臃肿。
同时降低了类与类之间的关系。
装饰类类因为增强已有对象,具备的功能和已有的功能是相同的,只不过提供了更强的功能。
java.io
类 LineNumberReaderjava.lang.Object
java.io.Reader
java.io.BufferedReader
java.io.LineNumberReader
public class LineNumberReader
extends BufferedReader跟踪行号的缓冲字符输入流。此类定义了方法 setLineNumber(int) 和 getLineNumber(),它们可分别用于设置和获取当前行号。
默认情况下,行编号从 0 开始。该行号随数据读取在每个行结束符处递增,并且可以通过调用 setLineNumber(int) 更改行号。但要注意的是,setLineNumber(int) 不会实际更改流中的当前位置;它只更改将由 getLineNumber() 返回的值。
可认为行在遇到以下符号之一时结束:换行符(’\n’)、回车符(’\r’)、回车后紧跟换行符
intgetLineNumber()
91a3
获得当前行号。
StringreadLine()
读取文本行。
voidsetLineNumber(int lineNumber)
设置当前行号。
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader(“D:\java\Pro\tset\asdf.txt” );
LineNumberReader inr = new LineNumberReader (fr);
String line = null;
inr.setLineNumber(100);
while (( line = inr.readLine()) != null) {
System. out.println( inr.getLineNumber() + “+” + line);
}
inr.close();
/*
自定义一个类,其中包含一个功能和readLine一致的方法。来模拟BufferedReader;
*/
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader( “D:\java\Pro\demo.txt”);
MyBufferedReader mbf = new MyBufferedReader( fr);
String line = null;
while (( line = mbf.myReadLine()) != null) {
System. out.println( line);
}
mbf.myClose();
}
}
class MyBufferedReader {
private FileReader r;
MyBufferedReader(FileReader r) { this. r = r; } public String myReadLine() throws IOException { // 定义一个临时容器,原BufferedReader封装的是字符数组。 // 为了演示方便,定义一个StringBuilder容器,因为最终还是要将字符变成字符串。 StringBuilder sb = new StringBuilder(); int ch = 0; while (( ch = r.read()) != -1) { if ( ch == '\r') continue; if ( ch == '\n') return sb.toString(); else sb.append(( char) ch); } return sb; } public void myClose() throws IOException { r.close(); }
}
装饰设计模式:当想要对已有的对象进行功能增强时,
可以定义类,将已有对象传入,基于已有的功能,并提供加强功能。
那么自定义的该类成为装饰类。
package tset1;
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException {
new SuperPerson(new Person()).superChifan();
}
}
class Person {
public void eat() {
System. out.println( “吃饭”);
}
}
class SuperPerson {
private Person p;
SuperPerson(Person p) { this. p = p; } public void superChifan() { System. out.println( "开胃酒"); p.eat(); System. out.println( "甜点"); System. out.println( "抽烟"); }
}
装饰和继承的区别:
装饰模式比继承灵活,避免继承体系的臃肿。
同时降低了类与类之间的关系。
装饰类类因为增强已有对象,具备的功能和已有的功能是相同的,只不过提供了更强的功能。
所以装饰类和被装饰类通常都属于一个体系中。
自定义装饰类java.io
类 LineNumberReaderjava.lang.Object
java.io.Reader
java.io.BufferedReader
java.io.LineNumberReader
public class LineNumberReader
extends BufferedReader跟踪行号的缓冲字符输入流。此类定义了方法 setLineNumber(int) 和 getLineNumber(),它们可分别用于设置和获取当前行号。
默认情况下,行编号从 0 开始。该行号随数据读取在每个行结束符处递增,并且可以通过调用 setLineNumber(int) 更改行号。但要注意的是,setLineNumber(int) 不会实际更改流中的当前位置;它只更改将由 getLineNumber() 返回的值。
可认为行在遇到以下符号之一时结束:换行符(’\n’)、回车符(’\r’)、回车后紧跟换行符
intgetLineNumber()
91a3
获得当前行号。
StringreadLine()
读取文本行。
voidsetLineNumber(int lineNumber)
设置当前行号。
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader(“D:\java\Pro\tset\asdf.txt” );
LineNumberReader inr = new LineNumberReader (fr);
String line = null;
inr.setLineNumber(100);
while (( line = inr.readLine()) != null) {
System. out.println( inr.getLineNumber() + “+” + line);
}
inr.close();
}
}
相关文章推荐
- 黑马程序员-Java IO和装饰设计模式
- 黑马程序员——Java基础——IO流、字符流、字节流、缓冲区技术、装饰设计模式、LineNumberReader等
- Java——设计模式(装饰模式_IO)
- java io 装饰设计模式
- java IO流 [缓冲技术] [装饰设计模式]
- 设计模式-装饰模式(Decorator)在jDK中IO系统的应用
- 12.IO(一)【IO流概述】【字符流】【字符流缓冲区】【装饰设计模式】【字节流】
- JAVA IO之装饰器模式(Head first 设计模式之装饰器模式)
- 设计模式之装饰模式与IO--JDK源码设计模式应用
- java IO 字节流、字节流读写文件、字节流缓冲区、文件拷贝、装饰设计模式
- 黑马程序员_温习 IO流一 (个人笔记) 简要(IO介绍。。字符流。。字符流缓冲区..装饰设计模式)
- 黑马程序员——学习Java基础之 io LineNumberReader 之装饰设计模式
- 黑马程序员-JAVA高级视频_IO输入与输出-19天-5(装饰设计模式)
- java学习之IO装饰设计模式
- java设计模式--装饰器设计模式(javaIO设计使用)
- java IO读写装饰设计模式
- java IO包 与 装饰设计模式 浅谈
- IO流二:IO流之缓冲区BufferedReader、BufferedWriter、装饰设计模式、LineNumberReader
- Java IO学习【10】装饰设计模式
- java IO之装饰设计模式