[英]Instance of abstract class InputStream
InputStream class defined as- InputStream类定义为-
public abstract class InputStream extends Object
Then how System class can contain "in" object of InputStream class ? 那么System类如何包含InputStream类的“ in”对象?
This is because in
is not an object, its a reference. 这是因为
in
不是对象,而是其引用。
Its a reference to an InputStream OR a sub-class of an InputStream. 它对InputStream的引用或InputStream的子类。 As its abstract, it can only be a sub-class.
作为其抽象,它只能是一个子类。
The in
field defined by the System
class doesn't need to reference a concrete implementation of InputStream
-- as you've worked out, it can't -- it just needs to reference something that extends InputStream
. 由
System
类定义的in
字段不需要引用InputStream
的具体实现-正如您已经解决了,它不能-只需引用扩展InputStream
。
On Linux at least, in
references a BufferedInputStream that itself wraps a FileInputStream. 在Linux上至少,
in
引用一个的BufferedInputStream本身包装一个FileInputStream。 Other implementations may vary and that's the point: the use of an abstract class like InputStream allows the implementing class to vary and potentially be changed from one version of Java to the next whilst keeping calling code happy. 其他实现可能会有所不同,这就是重点:使用InputStream之类的抽象类可以使实现类有所不同,并且有可能从Java的一个版本更改为另一个版本,同时保持调用代码的满意度。
An InputStream
is the abstract, but the concrete class (the one actually referenced by System.in
) can be any subclass of InputStream, including an anonymous class . InputStream
是抽象的,但是具体的类(由System.in
实际引用的类 )可以是InputStream的任何子类 ,包括匿名类 。
Some subclasses listed in the javadoc for InputStream
include: javadoc中为
InputStream
列出的一些子类包括:
Executing this code to find out which subclass System.in actual is: 执行此代码以找出实际上是哪个子类System .:
System.out.println(System.in.getClass());
Yields this answer: 产生此答案:
class java.io.BufferedInputStream
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.