看板 java 關於我們 聯絡資訊
先來段多年前寫的 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
foxenangel:所以錯誤訊息才會要我加上final,原來如此! 謝謝!! 02/23 14:11
tkcn:你這推文真是讓我很想去撞豆腐.... 02/23 15:05
foxenangel:我知道不是inner class啦 豆腐可以省下來.... 02/23 15:15