补充一个问题:在帮助文档是,read的声明是public abstract int read();System.in是InputStream类型的变量(帮助文档没说是InputStream的实例对象),为什么System.in在静态方法中可以不创建实例对象直接调用非静态类呢?难道System.in是Inputstream的某一个子类的实例对象?求高手解答,万分感谢,谢谢!!!!
看文档不如看源码:System类:/** * 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 = nullInputStream();/** * The following two methods exist because in, out, and err must be * initialized to null. The compiler, however, cannot be permitted to * inline access to them, since they are later set to more sensible values * by initializeSystemClass(). */ private static InputStream nullInputStream() throws NullPointerException { if (currentTimeMillis() > 0) { return null; } throw new NullPointerException(); }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));//这就是in setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));//这是out setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128), true));//这是err} 验证一下: System.out.println(System.in.getClass().getName());//输出:java.io.BufferedInputStream
继承InputStream的子类很多
AudioInputStream, ByteArrayInputStream, FileInputStream, FilterInputStream, ObjectInputStream, PipedInputStream, SequenceInputStream, StringBufferInputStream
各有不同的用途
is只是个接口,它一定有实现类。
我又查看了System.in的帮助文档,说System.in是InputStream类型的变量,也没有说指向那个子类的实例对象,如果真的指向了我觉得应该有提示。
我又查看了System.in的帮助文档,说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 = nullInputStream();/**
* The following two methods exist because in, out, and err must be
* initialized to null. The compiler, however, cannot be permitted to
* inline access to them, since they are later set to more sensible values
* by initializeSystemClass().
*/
private static InputStream nullInputStream() throws NullPointerException {
if (currentTimeMillis() > 0) {
return null;
}
throw new NullPointerException();
}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));//这就是in
setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));//这是out
setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128), true));//这是err}
验证一下:
System.out.println(System.in.getClass().getName());//输出:java.io.BufferedInputStream