1. 程式人生 > >java中什麽是局部內部類Local inner class?

java中什麽是局部內部類Local inner class?

can ati begin article 範圍 lin url -- 第一章

5.局部內部類Local inner class (視頻下載) (全部書籍)

馬克-to-win:什麽叫局部內部類?內部類聲明位置:1.它的外部類的範圍之內。2.在幾個程序塊的範圍之內。例如,由方法定義的塊中或甚至在for循環體內部。局部內部類有什麽意義?意義就是:你希望這個類只被解決某個問題用,任何其他人,其他地方都不能用它。就像臨時變量一樣。馬克-to-win:生活中百年不遇我們去海邊玩,專門裁出一塊布來鋪在沙灘上, 但這塊布幹什麽別的事都不合適,就屬於這種情況。

例2.5---本章源碼

class ShellMark_to_win {
int x = 100;
void test() {
for (int i = 0; i < 2; i++) {
/*馬克-to-win:for循環之外,Core類不存在。 outside of this for loop, inner can not be used. */
class Core {
void display() {
System.out.println("外部類的x=" + x);
}
}
Core inner = new Core();
inner.display();
}
// Core inner = new Core(); //錯誤找不到Core。
}
}
public class Test {
public static void main(String args[]) {
。。。。。。。。。。。。。。。。。
詳情請進:http://www.mark-to-win.com/index.html?content=JavaBeginner/javaUrl.html&chapter=JavaBeginner/JavaBeginner4_web.html#LocalInnerClass

java中什麽是局部內部類Local inner class?