2017-07-28 24 views
-2

InputStream是一个抽象类。那么我们怎样才能访问System.in.And而且int read()是InputStream类中的一个抽象方法。那么如果读取的话我们如何访问System.in.read()方法()是一种抽象方法。 像是System.in中的InputStream类的对象引用?

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

也许看看[this](http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html)... – Lino

+0

你应该检查System类中的initializeSystemClass()方法,它包含这些Streams如何初始化的细节。 – dvsakgec

+0

我不认为这是一个坏问题。一个人不应该投票。 – Joshua

回答

0

被System.in中的InputStream类的对象引用?

是的!特此声明/记录在系统类:

/** 
* 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类的引用

所以OU不实例化一个抽象类 enter image description here

+0

在你看到的实现中。规范中没有任何内容保证这一点。 – EJP

+0

你的意思是平台依赖? –

+0

我没有对平台说过一句话。它由实现来定义,而不是由规范来定义。 – EJP

2

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

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

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