A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 马蚁牙黑 中级黑马   /  2016-8-22 23:54  /  1014 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

FileInputStream中的read()方法是对抽象类InputStream中read()的实现,还是自己另定义了一个read()方法呢求解

6 个回复

倒序浏览
FileInputStream把InputStream的read方法继承了。
回复 使用道具 举报
InputStream  中的read()方法根据底层代码可以看出是没有定义方法体的抽象方法,  FileInputStream子类继承了父类的这个方法, 并写重写了这个方法,定义了方法体.
回复 使用道具 举报
是继承父类的.要重写
回复 使用道具 举报
[AppleScript] 纯文本查看 复制代码
 
/*
这是FileInputStream底层的关于read()方法的源码,也就是说FileInputStream继承了InputStream,并且在底层重写了InputStream中的read方法,在调用的时候,,底层已经帮你把read方法给写好了,就没必要纠结要不要去重写read方法了,个人理解

*/
public int read() throws IOException {
        Object traceContext = IoTrace.fileReadBegin(path);
        int b = 0;
        try {
            b = read0();
        } finally {
            IoTrace.fileReadEnd(traceContext, b == -1 ? 0 : 1);
        }
        return b;
    }

    private native int read0() throws IOException;

    /**
     * Reads a subarray as a sequence of bytes.
     * @param b the data to be written
     * @param off the start offset in the data
     * @param len the number of bytes that are written
     * @exception IOException If an I/O error has occurred.
     */
    private native int readBytes(byte b[], int off, int len) throws IOException;

    /**
     * Reads up to <code>b.length</code> bytes of data from this input
     * stream into an array of bytes. This method blocks until some input
     * is available.
     *
     * @param      b   the buffer into which the data is read.
     * @return     the total number of bytes read into the buffer, or
     *             <code>-1</code> if there is no more data because the end of
     *             the file has been reached.
     * @exception  IOException  if an I/O error occurs.
     */
    public int read(byte b[]) throws IOException {
        Object traceContext = IoTrace.fileReadBegin(path);
        int bytesRead = 0;
        try {
            bytesRead = readBytes(b, 0, b.length);
        } finally {
            IoTrace.fileReadEnd(traceContext, bytesRead == -1 ? 0 : bytesRead);
        }
        return bytesRead;
    }
回复 使用道具 举报
InputStream 里的read()是抽象方法,FileInputStream里的read()重写了它,然后在read()方法里调用read0()这个原生态方法。
回复 使用道具 举报
兄台!看着你这么单纯好学,我推荐你最好凡事不懂就问百度吧!亲
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马