没有抽象方法的抽象类,存在什么样的意义呢?
2012-05-06 19:14
477 查看
都知道抽象类可以没有抽象方法,这时的抽象类和普通类的最大差别就是不能实例化.一直没找到在什么环境下用到这种特殊的抽象类.最近看了jdk的I/O包后,发现里面有个很好的例子,就是FilterReader类.以下是FilterReader的代码:
可以看到FilterReader就是一个没有抽象方法的抽象类,里面的每个方法都是调用构造函数传入的Reader对象的方法.这种抽象类你不能实例化它,因为实例化它没意义,它还没实现任何Filter的功能.在extends具体子类时实现Filter功能,实例化相应的子类才有实际意义.
我们可以写FilterReader的一个具体子类如下:
public abstract class FilterReader extends Reader ...{ /** *//** * The underlying character-input stream. */ protected Reader in; /** *//** * Create a new filtered reader. * * @param in a Reader object providing the underlying stream. * @throws NullPointerException if <code>in</code> is <code>null</code> */ protected FilterReader(Reader in) ...{ super(in); this.in = in; } /** *//** * Read a single character. * * @exception IOException If an I/O error occurs */ public int read() throws IOException ...{ return in.read(); } /** *//** * Read characters into a portion of an array. * * @exception IOException If an I/O error occurs */ public int read(char cbuf[], int off, int len) throws IOException ...{ return in.read(cbuf, off, len); } /** *//** * Skip characters. * * @exception IOException If an I/O error occurs */ public long skip(long n) throws IOException ...{ return in.skip(n); } /** *//** * Tell whether this stream is ready to be read. * * @exception IOException If an I/O error occurs */ public boolean ready() throws IOException ...{ return in.ready(); } /** *//** * Tell whether this stream supports the mark() operation. */ public boolean markSupported() ...{ return in.markSupported(); } /** *//** * Mark the present position in the stream. * * @exception IOException If an I/O error occurs */ public void mark(int readAheadLimit) throws IOException ...{ in.mark(readAheadLimit); } /** *//** * Reset the stream. * * @exception IOException If an I/O error occurs */ public void reset() throws IOException ...{ in.reset(); } /** *//** * Close the stream. * * @exception IOException If an I/O error occurs */ public void close() throws IOException ...{ in.close(); } }
可以看到FilterReader就是一个没有抽象方法的抽象类,里面的每个方法都是调用构造函数传入的Reader对象的方法.这种抽象类你不能实例化它,因为实例化它没意义,它还没实现任何Filter的功能.在extends具体子类时实现Filter功能,实例化相应的子类才有实际意义.
我们可以写FilterReader的一个具体子类如下:
/** * FilterReader是一个抽象类,不能实例化该类,FilterReader类的每个方法都是实现的, * 里面调用的都是FilterReader(Reader in)的传入参数in的方法. */ public class UppercaseConvertor extends FilterReader...{ // 构造方法由FilterReader的protected上升到public的 public UppercaseConvertor(Reader in) ...{ super(in); } @Override public int read() throws IOException...{ int c = super.read(); return (-1==c?c:Character.toUpperCase((char)c)); } @Override public int read(char[] buf,int offset, int count) throws IOException...{ int nread = super.read(buf, offset, count); int last = offset + nread; for(int i=0;i<last;i++) buf[i] = Character.toUpperCase(buf[i]); return nread; } public static void main(String[] args) throws IOException ...{ UppercaseConvertor uc = new UppercaseConvertor(new FileReader("d:\log.txt")); BufferedReader br = new BufferedReader(uc); String r; while(null!=(r=br.readLine()))...{ System.out.println(r); } } }
相关文章推荐
- 没有抽象方法的类,可以申明为抽象方法吗?有抽象方法的类,一定要申明为抽象类。。
- C#抽象类与抽象方法--就是类里面定义了函数而函数里面什么都没有做的类
- 面向对象,类的组合关系,继承,实现,方法重写,方法重载,this的使用,抽象方法和抽象类的比较,父类构造方法存在的意义,多态的是用和解析,各种访问修饰符
- 抽象类可以没有抽象方法,不能被实例化
- 抽象类没有抽象方法
- Java中没有抽象方法的抽象类
- 适配器模式以及为什么允许声明没有抽象方法的类为抽象类?
- 《黑马程序员》java笔记->【03】没有抽象方法的抽象类
- C#类、接口、虚方法和抽象方法-非抽象类与接口的差别
- Dt大数据梦工厂王家林老师 Scala实战详解之第13讲 抽象类、抽象字段、抽象方法
- PHP 抽象方法与抽象类 abstract 关键字
- 抽象类与抽象方法
- 用ActionScript3实现抽象类和抽象方法及单态模式
- 2.创建一个抽象类A,该类中包含一个求两个数之和抽象方法。创建一个子类B,在B中重写求和方法,且使用方法重载使得方法可以分别计算整数、双精度、字符串
- php中的抽象方法和抽象类
- 抽象类 接口 抽象方法 虚方法【基本说明】
- C# 抽象类与抽象方法
- 抽象类继承抽象类可以不重写父类的抽象方法,普通类继承抽象类必须重写父类中的抽象方法,实现接口也是同样的道理
- C#中的虚方法、抽象方法、抽象类、接口的联系与区别
- 2.15.PHP7.1 女神级教程-女神的私人信息 -【PHP 抽象类、抽象方法】