Re: [問題] 關於final

看板java作者 (小安)時間16年前 (2010/02/23 13:53), 編輯推噓2(201)
留言3則, 2人參與, 最新討論串2/3 (看更多)
先來段多年前寫的 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
所以錯誤訊息才會要我加上final,原來如此! 謝謝!!
02/23 14:11, 1F

02/23 15:05, , 2F
你這推文真是讓我很想去撞豆腐....
02/23 15:05, 2F

02/23 15:15, , 3F
我知道不是inner class啦 豆腐可以省下來....
02/23 15:15, 3F
文章代碼(AID): #1BWsrtyr (java)
文章代碼(AID): #1BWsrtyr (java)