เมื่อฉันอ่านซอร์สโค้ดจาก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;
}
มีใครสามารถให้คำอธิบายที่สมเหตุสมผลได้หรือไม่?
ifคำสั่งไม่ได้จริงๆเหรอ?
Eclipseคุณไม่สามารถหยุดดีบักเกอร์ชั่วคราวในifคำสั่งได้ อาจเป็นเหตุผลสำหรับตัวแปรนามแฝงนั้น แค่อยากจะโยนมันออกไป แน่นอนฉันคาดเดา