Chapter8

1.編譯包含一個內隱類別的類別後會產生兩個.class

2.執行內隱類別只能透過外部類別的參考

3.要在內隱類別中用到外部類別的this,則要用 外部類別名稱.this

4.內隱類別不能使用被包含在其中的函式的變數(因為內隱類別可能會活得比它久),除非該變數宣告成final

5.匿名內隱類別
class Popcorn{
public void pop(){
System.out.println("popcorn");
}
}

class Food{
Popcorn p = new Popcorn(){
public void pop(){
System.out.println("anomynous popcorn");
}
};
}

new Popcorn()後面沒接;,表示後面寫的是匿名內隱類別,如果Popcorn是實體類別,則該匿名內隱類別就是Popcorn的子類別,如果Popcorn是介面,則該匿名內隱類別就是implmenter,擴充該interface。
原來的;跑到內隱類別結束的大括號後面。

6.在引數宣告匿名內隱類別
class MyWonderfulClass{
void go(){
Bar b = new Bar(){
b.doStuff(new Foo() {
public void foof(){
System.out.println("foofy");
}

});
}
}
}

interface Foo{
void foof();
}

class Bar{
void doStuff(Foo f){}
}

在doStuff的引數宣告匿名內隱類別implement Foo,把整個引數用匿名內隱類別取代,且最後還是要加上;,因為是呼叫函式的結尾。


Advantages:
1.The object-oriented advantage
Algorithms and other codes could decoupled more

2.The organazation advantage
Package P
class OutClass
class a
class b
class InnerC
classd
...

3.The callback advantage
class a{
botten1.addActionListener(
anomynous inner class..
)
...
}


Disadvantages:

May hard to be maintained or understanded by inexperienced programmer.

Most develop tools come a bit little support inner classes.


考題:
第三,Static Nested Class 和 Inner Class的不同,說得越多越好。

Static Nested Class不能直接參考它的封裝類別(enclosing class)中的實
體變數或方法,僅能透過object instance的方式存取。
Inner Class可以直接存取它的封裝類別(enclosing class)中的變數或方法。
inner class中不能定義任何的static成員。


swangs
swangs
Latest page update: made by swangs , Sep 20 2007, 5:00 AM EDT (about this update About This Update swangs Edited by swangs

18 words added

view changes

- complete history)
Keyword tags: None
More Info: links to this page
Top Contributors