2016-07-22 27 views
0

read()方法在InputStream類中聲明爲抽象類。但是我們可以使用System.in.read()從鍵盤讀取。System.in默認引用了什麼?

我的問題是'in'是InputStream類的參考。所以要用read()的方法必須參考InputStream的一些小類。

默認情況下'in'是指什麼類?我們可以寫一個代碼來找到這個問題的答案嗎?

+0

它是指**。查看添加的[Javadoc](https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#in)*此流已經打開並準備好提供輸入數據。通常,此流對應於由主機環境或用戶指定的鍵盤輸入或其他輸入源。* –

回答

3

爲了回答您的具體問題:是的,你可以通過編寫代碼找出類的System.in

這是你正在尋找應用程序:

public class SystemDotInClassFinder { 
    public static void main(String[] args) { 
     System.out.println(System.in.getClass().getName()); 
    } 
} 

運行此腳本生成:到**標準輸入

java.io.BufferedInputStream 
1

正如你可以在源代碼中看到它是一個的BufferedInputStream:

private static void initializeSystemClass() { 
    props = new Properties(); 
    initProperties(props); 
    VM.saveAndRemoveProperties(props); 
    lineSeparator = props.getProperty("line.separator"); 
    Version.init(); 
    FileInputStream arg = new FileInputStream(FileDescriptor.in); 
    FileOutputStream arg0 = new FileOutputStream(FileDescriptor.out); 
    FileOutputStream arg1 = new FileOutputStream(FileDescriptor.err); 
    setIn0(new BufferedInputStream(arg)); 
    setOut0(newPrintStream(arg0, props.getProperty("sun.stdout.encoding"))); 
    setErr0(newPrintStream(arg1, props.getProperty("sun.stderr.encoding"))); 
    loadLibrary("zip"); 
    Terminator.setup(); 
    VM.initializeOSEnvironment(); 
    Thread arg2 = Thread.currentThread(); 
    arg2.getThreadGroup().add(arg2); 
    setJavaLangAccess(); 
    VM.booted(); 
}