作者tkcn (小安)
看板java
标题Re: [问题] 关於final
时间Tue Feb 23 13:53:56 2010
先来段多年前写的 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
1F:推 foxenangel:所以错误讯息才会要我加上final,原来如此! 谢谢!! 02/23 14:11
2F:→ tkcn:你这推文真是让我很想去撞豆腐.... 02/23 15:05
3F:推 foxenangel:我知道不是inner class啦 豆腐可以省下来.... 02/23 15:15