简体   繁体   English

Java:从 System.in 读取时,InputStream 的 read() 方法在哪里实现

[英]Java: Where is InputStream's read() method implemented when reading from System.in

Abstract class InputStream says that subclasses need to implement method read() which reads one byte and then turns it into an unsigned int .抽象类InputStream表示子类需要实现read()方法,该方法读取one byte ,然后将其转换为unsigned int

System.in is an InputStream and I can do: System.in是一个InputStream ,我可以这样做:

 int i = System.in.read();

My question is.. where is this method implemented?我的问题是..这个方法在哪里实现? How come it works?它是怎么工作的? Maybe an odd question but I'm trying to find out what's happening under the hood and since I'm using an object of class InputStream and not one of its subclasses, I'm wondering where the actual method is implemented and why it works...也许是一个奇怪的问题,但我试图找出幕后发生的事情,因为我使用的是 InputStream 类的对象而不是它的子类之一,我想知道实际方法在哪里实现以及它为什么起作用。 ..

System.in is an implementation of an InputStream which has the read method implemented. System.in 是 InputStream 的一个实现,它实现了 read 方法。

If you take a look in the source of System you can see it's really a FileInputStream opening the file handle 0 (which is the standard input stream).如果您查看System的源代码,您会发现它实际上是一个FileInputStream打开文件句柄 0(这是标准输入流)。 But this isn't really relevant.但这并不重要。 The only thing you need to know is that you can read from it.您唯一需要知道的是您可以从中读取。 It isn't relevant if its a FileInputStream , a StringInputStream or some other native InputStream .如果它是FileInputStreamStringInputStream或其他一些本机InputStream That's the whole point of interfaces.这就是接口的全部意义所在。 You can mostly just program against an interface without worrying about the specific implementation.大多数情况下,您可以只针对接口进行编程,而不必担心具体的实现。

What's happening under the hood is this: the in attribute of the class System is assigned, at runtime, a reference to an object of some class X .幕后发生的事情是:类Systemin属性在运行时被分配了对某个类X的对象的引用。 That class X extends InputStream , and as such, it implements the read method.该类X扩展了InputStream ,因此它实现了read方法。

You can't tell for sure (meaning, it's not specified anywhere) what class X is;你不能确定(意思是,它没有在任何地方指定) X是什么类; all you know is that it extends InputStream .你所知道的是它扩展了InputStream Different JDK's might use different classes.不同的 JDK 可能使用不同的类。

InputStream is the type of System.in , and not it's class (since InputStream cannot be directly instantiated as it is abstract). InputStreamSystem.in的类型,而不是它的类(因为InputStream不能直接实例化,因为它是抽象的)。

Consider:考虑:

Object obj = "123";

The type of the variable obj is Object , but the instance referenced by obj is an instance of String .变量obj的类型是Object ,但obj引用的实例是String的实例。 When toString() is called on obj the implementation in String is used, and not the implementation in Object .当对obj调用toString() ,将使用String的实现,而不是Object的实现。

The same goes for System.in . System.in The actual instance stored there will be some subclass of InputStream , which will have its own implementation of any abstract methods.存储在那里的实际实例将是InputStream一些子类,它将有自己的任何抽象方法的实现。 If you want to know the class of the instance stored in System.in then you can call System.in.getClass() .如果您想知道System.in存储的实例的类,则可以调用System.in.getClass()

What is System.in.read() ?什么是 System.in.read() ?

1) System.in returns static InputStream in 1) System.in返回static InputStream in

First of all InputStream Class is an abstract class with its read method also abstract , this means there could be n number of implementations for the read method .首先InputStream Class 是一个abstract class ,它的read method also abstract ,这意味着read method also abstract可能有 n 个实现。

So whats happening behind the scenes ?那么幕后发生了什么?

System.in returns an instance of InputStream class on runtime ie class which extends InputStream as we know all byte stream classes extends InputStream , so it can be any class extending InputStream class. System.in InputStream class on runtime返回InputStream class on runtime的实例,即扩展 InputStream 的类,因为我们知道所有字节流类都扩展 InputStream ,因此它可以是扩展 InputStream 类的任何类。 Now the read method is called from that class which it actually extending InputStream.现在read method is called from that class which it actually extending InputStream.

You can always check the instance provided for InputStream class by calling System.in.getClass()您始终可以通过调用System.in.getClass()检查为 InputStream 类提供的实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM