先來段多年前寫的 SCJP 筆記:
---
Anonymous Inner Classes 也算是一種 Inner Class。
假設 Popcorn 是一個 Class:
Popcorn p = new Popcorn() {
public void pop() {
System.out.println("anonymous popcorn");
}
};
The Popcorn reference variable refers not to an instance of Popcorn,
but to an instance of an anonymous (unnamed) subclass of Popcorn.
但由於 p 的 Reference Type 是 Popcorn,
所以不能透過 p invoke 任何新的 (非 override) method。
Interface 和 Abstract Class 也適用 Anonymous Inner Classes。
Anonymous Inner Classes 甚至能夠出現在參數列:
class MyWonderfulClass {
void go() {
Bar b = new Bar();
b.doStuff(new Foo() {
public void foof() {
System.out.println("foofy");
} // end foof method
}); // end inner class def, arg, and end statement
} // end go()
} // end class
---
另外你也問了怎麼樣把 valueChanged 移到外面的 scope
印象中螞蟻書在 Thread 的章節,
有提到三種 繼承/實作 Thread/Runnable 的方法,
那個章節建議你可以去看看。
ps. 我的螞蟻書應該是第五版的,而且 6 年沒看了,萬一記錯了請見諒
※ 引述《foxenangel (該來的還是跑不掉阿)》之銘言:
: add(messageLabel, BorderLayout.SOUTH);
: tree.addTreeSelectionListener(new TreeSelectionListener() {
: public void valueChanged(TreeSelectionEvent e) {
: TreePath tp=e.getNewLeadSelectionPath();
: messageLabel.setText("Selected: "+tp.getLastPathComponent() );
: } ^^^^^^^^^^^^
: });
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.122.183.199
推
02/23 14:11, , 1F
02/23 14:11, 1F
→
02/23 15:05, , 2F
02/23 15:05, 2F
推
02/23 15:15, , 3F
02/23 15:15, 3F
討論串 (同標題文章)