抽象方法不是没有方法体吗?不是必须重写才能使用吗?为什么抽象类InputStream中的抽象方法Read()可以直接调来使用,而且实现了一定的功能?

解决方案 »

  1.   

    建议你还是看看InputStream的api
    继承InputStream的子类很多
    AudioInputStream, ByteArrayInputStream, FileInputStream, FilterInputStream, ObjectInputStream, PipedInputStream, SequenceInputStream, StringBufferInputStream 
    各有不同的用途
      

  2.   

    如果InputStream类型的引用指向了其子类的实例对象,那么调用read时并不是调用它自己的read,而是调用其子类的方法。
      

  3.   

    关键是你拿到的实例一定不是InputStream,而是InputStream的子类。里面肯定有实现read方法
      

  4.   

    希望楼主多学习JAVA基础。
    is只是个接口,它一定有实现类。
      

  5.   

    补充一个问题:在帮助文档是,read的声明是public abstract int read();System.in是InputStream类型的变量(帮助文档没说是InputStream的实例对象),为什么System.in在静态方法中可以不创建实例对象直接调用非静态类呢?难道System.in是Inputstream的某一个子类的实例对象?求高手解答,万分感谢,谢谢!!!!
      

  6.   

    是的,我就是这里疑惑,我看的是清华大学的java课本,它说System.in调用的是InputSteam的抽象方法read,而帮助文档中InputStream中的抽象方法read的确实现了一定的功能,抽象方法怎么能实现功能呢?
    我又查看了System.in的帮助文档,说System.in是InputStream类型的变量,也没有说指向那个子类的实例对象,如果真的指向了我觉得应该有提示。
      

  7.   

    是的,我就是这里疑惑,我看的是清华大学的java课本,它说System.in调用的是InputSteam的抽象方法read,而帮助文档中InputStream中的抽象方法read的确实现了一定的功能,抽象方法怎么能实现功能呢?
    我又查看了System.in的帮助文档,说System.in是InputStream类型的变量,也没有说指向那个子类的实例对象,如果真的指向了我觉得应该有提示。
      

  8.   

    看文档不如看源码:System类:/**
         * The "standard" input stream. This stream is already
         * open and ready to supply input data. Typically this stream
         * corresponds to keyboard input or another input source specified by
         * the host environment or user.
         */
    public final static InputStream in = nullInputStream();/**
         * The following two methods exist because in, out, and err must be
         * initialized to null.  The compiler, however, cannot be permitted to
         * inline access to them, since they are later set to more sensible values
         * by initializeSystemClass().
         */
    private static InputStream nullInputStream() throws NullPointerException {
    if (currentTimeMillis() > 0) {
        return null;
    }
    throw new NullPointerException();
        }private static void initializeSystemClass() {
       。。
            FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
    FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
    FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);
    setIn0(new BufferedInputStream(fdIn));//这就是in
    setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));//这是out
    setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128), true));//这是err}
    验证一下:
    System.out.println(System.in.getClass().getName());//输出:java.io.BufferedInputStream
      

  9.   

    InputStream是不能直接调用的,必须要实现,read方法在子类中已经实现,Lz还是先把基础弄好