System.in是InputStream类的对象引用吗?

时间:2017-07-28 10:10:39

标签: java io system.in

InputStream是一个Abstract类。然后我们如何能够访问System.in.而且int read()是InputStream类中的一个抽象方法。那么我们如何才能访问System.in.read()方法if read()是一种抽象方法。 喜欢

int a=System.in.read();
System.out.println((char)a); 

3 个答案:

答案 0 :(得分:3)

我建议你阅读更多关于Java中的抽象和继承方法。

如果扩展抽象类,则必须实现其抽象方法。这样,您就可以为它提供一个实现,可以由消费者调用。

System.in是一个类的实例,它直接扩展InputStream,而不是InputStream

答案 1 :(得分:0)

  

是System.in是InputStream类的对象引用吗?

是的!,它在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 = null;

但在运行时是对BufferedInputStream

的引用

所以你不是在实例化一个抽象类 enter image description here

答案 2 :(得分:0)

abstractSystem.in类,不能直接实例化。 InputStream指类型为System.in的对象,这意味着extends指的是InputStreamabstract class IAmAbstract{ // ... } class IAmNotAbstract extends IAmAbstract{ // ... } 类的该类的对象。

例如

IAmNotAbstract obj = new IAmNotAbstract();

当然,以下说法是正确的:

IAmAbstract obj = new IAmNotAbstract();

这句话也是正确的:

subclass

因此,InputStream的{​​{1}}的任何对象也是InputStream类的类型和subclass本身。