Java内部类访问外部对象的方法
Java编程时,为类DialogTry2添加关闭窗口事件,我在构造方法中采用事件适配器来实现:
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
this.dispose();
}
});
本意是为当前窗口(实际上为一个对话框)添加关闭窗口事件。但编译器报错说,new WindowAdapter()不存在dispose()方法。我明白过来,this.dispose()中的this不是窗口对象,而是适配器对象。那么怎样调用外部对象即窗口对象呢?既不能用super也没有outer。上网查一下,找到一个方法:在这个复合语句外面加一句:
final DialogTry2 outer = this;
然后把this.dispose();改为:
outer.dispose();
这样就好了。也看明白了怎么回事。原来this作为当前的对象可以这样调用啊,长见识了。
改好的程序如下:
final DialogTry2 outer = this;
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
outer.dispose();
}
});
不记得从哪个网页看到的了,那页很复杂,讲了许多问题,我只从其中摘出这一条来。感谢那位网友。
相关文档:
1)垃圾回收的两个关键要素:
发现无用对象。
回收无用对象的内存空间。
2)6种垃圾回收算法:
引用计数法,tracing 算法,compacting算法,copying 算法,generation算法,adaptive算法。
3)detail:
引用计数法(Reference Counting Collector)
引用计数法是唯一没有使用根集的垃圾回收的 ......
SAMPLE:
import java.util.Properties;
Properties props=System.getProperties(); //获得系统属性集
String osName = props.getProperty("os.name"); //操作系统名称
String osArch = props.getPropert ......
关键字: filter
过滤器Filter也具有生命周期:init()->doFilter()->destroy(),由部署文件中的filter元素驱动。在servlet2.4中,过滤器同样可以用于请求分派器,但须在web.xml中声明,<dispatcher>INCLUDE或FORWARD或REQUEST或ERROR</dispatcher>该元素位于filter ......
The Java virtual machine defines various runtime data areas that are used during execution of a program. Some of these data areas are created on Java virtual machine start-up and are destroyed only when the Java virtual machine exits. Other data areas are per thread. Per-thread data areas are create ......