public final class System{...}
问题:
1、native是怎么用的?
比如在java的System类中,有这样一个函数:
private static native void registerNatives();
现在知道,native方法是在我的java程序外面用其它语言(比如C、C++、汇编等实现的!应该自己试试看!)
2、static块是怎么回事?
比如在java的System类中有这样一个块:
static {
registerNatives();
}
3、至于迷惑很久的System.in是什么?
解答:
在java的System中,in成员是这样定义的:
public final static InputStream in = null;
那么,InputStream是怎么回事?
在java中,InputStream是如下这样定义的:
public abstract class InputStream implement Closeable{...}
在implements Closeable中,仅仅只有一个close方法,并且也是其它接口的!
数据成员有:
private static final int MAX_SKIP_BUFFER_SIZE = 2048;
成员方法有:
public abstract int read();
public int read(byt b[]) throws IOException{ return read(b, 0, b.length()); }
public int read(byte b[], int off, int len) throws IOException {...}
public long skip (long n) throws IOException { long remaining = n; if (n< 0) return 0; int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining); byte[] skipBuffer = new byte[size]; while (remaining > 0) { nr = read(skipBuffer, 0, (int)Math.min(sie, remaining)); if (nr < 0) break; remaining -= nr; } return n - remianing; }
这个skip方法是干嘛的?
public int available() throws IOException { return 0; }
这个available方法是干嘛的?
public void close() throws IOException{}
public synchronized void mark(int readlimit);
public sychronized void reset() throws IOException() { throws new IOException("mark/reset not supported");}
上面的mark和reset方法是干嘛的?关键字synchronized是怎么用的?
public boolean markSupported() { return false; }
这个方法markSupported是干嘛的?
到此为止,InputStream有一点熟悉了,先继续看!~
4、System.out是怎么回事?
解答:
在java的System类中,out成员的定义如下:
public final static PrintStream out = null;
那么,PrintStream是怎么回事?
在java中,PrintStream如下定义:
public class PrintStream extends FileterOutputStream
implements Appendable, Closeable{}
public class FilterOutputStream extends OutputStream{}
public class OutputStream implements Closeable, Flushable{}
关于OutputStream的实现如下:
public abstract class OutputStream implements Closeable, Flushable { public abstract void write(int b) throws IOException; public void write(byte b[]) throws IOException { write(b, 0, b.length); } public void write(byte b[], int off, int len) throws IOException { if (b == null) { throw new NullPointerException(); } else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return; } for (int i = 0 ; i < len ; i++) { write(b[off + i]); } } public void flush() throws IOException { } public void close() throws IOException { } }
在void write(byte b[], int off, int len)方法中,有这样一条语句:
write(b[off+i]);
这条语句不就是调用方法write(int b)吗?但是这个方法显然是abstract的啊,怎么可以调用呢?
难道说:
在一个类中,某个方法可以调用该类的另一个abstract方法,以实现某种功能?不可能啊,abstract方法连方法体都没有!?
上面的InputStream有同样的疑问!
今天就到这里吧~!