Discuss / Java / 提问

提问

Topic source

拆开宇宙

#1 Created at ... [Delete] [Delete and Lock User]

 public int read(byte[] b, int off, int len) throws IOException {

        int n = in.read(b, off, len);

        this.count += n;

        return n;

    }

这段我删掉之后依然得出之前的结果,想问问作用。

因为你不知道调用的人会调用read()还是read(byte[] b, int off, int len),不管调用哪个方法,都要更新count的值

测试给的例子是读取单个字节,但read方法一般还有个重载的方法,是读取字节数组的,你不知道调用者到底一次读取一个字节还是读取一个字节数组

public int read(byte[] b, int off, int len) throws IOException {

        int n = in.read(b, off, len);

        this.count += n;

        return n;

    }

不太懂这两个函数里的in.read()调用的到底是哪个read函数,是现在构造的函数read,还是之前inputstream自己本身就有的read函数呢?

茂茂_916

#5 Created at ... [Delete] [Delete and Lock User]

楼上的问题,你需要知道in现在到底是哪个类的实例,main主方法中调用构造函数的代码就可以解决你的疑问。

这只是一个重载方法

这段我删掉之后依然得出之前的结果

说这句应该去补一补基础 没有其他意思


  • 1

Reply