当我阅读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;
}
有人能给出一个合理的解释吗?
https://stackoverflow.com/questions/36231047
复制相似问题