最佳答案
当我阅读 java.io.BufferedInputStream.getInIfOpen()
的源代码时,我对它为什么写这样的代码感到困惑:
/**
* Check to make sure that underlying input stream has not been
* nulled out due to close; if not return it;
*/
private InputStream getInIfOpen() throws IOException {
InputStream input = in;
if (input == null)
throw new IOException("Stream closed");
return input;
}
为什么它使用别名而不直接使用字段变量 in
,如下所示:
/**
* Check to make sure that underlying input stream has not been
* nulled out due to close; if not return it;
*/
private InputStream getInIfOpen() throws IOException {
if (in == null)
throw new IOException("Stream closed");
return in;
}
有人能给个合理的解释吗?