国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

通過Java IO流的形式實現(xiàn)鍵盤錄入的疑問

瀏覽:72日期:2023-12-31 15:18:24

問題描述

通過Java IO流的形式實現(xiàn)鍵盤錄入的疑問

問題解答

回答1:

因為System.in就是一個靜態(tài)的InputStream實例,你可以在java api文檔上看看

回答2:

首先,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;

可見System.in屬于標準輸入,可以通過鍵盤或其他方式輸入數(shù)據(jù)。但在源碼里,該對象并沒有(顯式)初始化的方法,通過閱讀源碼,可發(fā)現(xiàn)下面這個方法:

/** * Reassigns the 'standard' input stream. * * <p>First, if there is a security manager, its <code>checkPermission</code> * method is called with a <code>RuntimePermission('setIO')</code> permission * to see if it’s ok to reassign the 'standard' input stream. * <p> * * @param in the new standard input stream. * * @throws SecurityException *if a security manager exists and its *<code>checkPermission</code> method doesn’t allow *reassigning of the standard input stream. * * @see SecurityManager#checkPermission * @see java.lang.RuntimePermission * * @since JDK1.1 */public static void setIn(InputStream in) { checkIO(); setIn0(in);}private static native void setIn0(InputStream in);

閱讀注釋可見該方法用于設(shè)定研究setIn0(in),可見該方法是通過調(diào)用底層接口來實現(xiàn)in的設(shè)定,那么在軟件運行時,是如何初始化的呢?System類中有如下代碼:

/* register the natives via the static initializer. * * VM will invoke the initializeSystemClass method to complete * the initialization for this class separated from clinit. * Note that to use properties set by the VM, see the constraints * described in the initializeSystemClass method. */private static native void registerNatives();static { registerNatives();}/** * Initialize the system class. Called after thread initialization. */private static void initializeSystemClass() {...FileInputStream fdIn = new FileInputStream(FileDescriptor.in);FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);setIn0(new BufferedInputStream(fdIn));setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128), true));...}

由此段代碼可知,軟件運行時,先運行靜態(tài)代碼塊,調(diào)用registerNatives()這個底層方法對System類進行初始化,該方法則是調(diào)用initializeSystemClass()方法來初始化System類的,這兩步都是通過VM實現(xiàn)的,然后閱讀initializeSystemClass()這個方法,可見到調(diào)用setIn0,setOut0,steErr0三段代碼,分別初始化了標準輸入,標準輸出,標準錯誤三種輸出流,至此System.in、out、err的初始化流程就都弄清楚了。標記為native的方法是JVM調(diào)用其他代碼實現(xiàn)的功能,這個和底層有關(guān)系,我覺得這個感覺就像是Bootstrap Classloader實際上是C實現(xiàn),但由JVM調(diào)用來加載各個基礎(chǔ)JAR classes相似吧。另外你說InputStream是抽象類(接口),實際上接口類抽象類都可以作為參數(shù),但實現(xiàn)肯定不是他們實現(xiàn)的,他們不能被實現(xiàn),但可以作為參數(shù),畢竟父類出現(xiàn)的地方子類都可以替代,所以這里不存在問題

標簽: java
相關(guān)文章:
主站蜘蛛池模板: 成年人视频免费网站 | 一级特黄aa大片欧美 | 九九99久麻豆精品视传媒 | 亚洲精品天堂自在久久77 | 国产美女又黄又爽又色视频免费 | 久久国产乱子伦精品免费不卡 | 最新国产三级在线不卡视频 | 1204国产成人精品视频 | 久草在线视频免费播放 | 国产午夜精品不卡视频 | 大桥未久在线精品视频在线 | 黄色一级片a | 69性欧美 | 99秒拍福利大尺度视频 | 超级碰碰碰视频视频在线视频 | 日本韩国台湾香港三级 | 免费国产高清精品一区在线 | 日韩一级黄色毛片 | 女人精aaaa片一级毛片女女 | 特级做人爱c级特级aav毛片 | 97久久天天综合色天天综合色 | 手机毛片免费看 | 国产成人盗拍精品免费视频 | 五月色一区二区亚洲小说 | 精品在线一区 | 一级毛片免费完整视频 | 久久91精品国产91久久小草 | 欧美日韩综合网在线观看 | 国产手机免费视频 | 国产三级日产三级日本三级 | 爽爽日本在线视频免费 | 男女上下爽无遮挡午夜免费视频 | 欧美老头老太做爰xxxx | 亚洲视频在线视频 | 日本成人午夜 | 精品国产高清a毛片 | 韩国免费毛片在线看 | 99视频免费观看 | 久久亚洲精品中文字幕 | 国产精品久久久久久久人热 | cao草棚视频网址成人 |