luxifa0425
发贴: 3
积分: 0
|
于 2007-11-19 17:28
class DoBaseFinalization { public static boolean flag=false; }
class Base { Base() { System.out.println("Base()"); } protected void finalize() throws Throwable { System.out.println("Base finalized()!"); if(DoBaseFinalization.flag) super.finalize(); } }
class Second extends Base { Second() { System.out.println("Second()"); } protected void finalize() throws Throwable { System.out.println("Second finalized()!"); if(DoBaseFinalization.flag) super.finalize(); } }
class Third extends Second { Third() { System.out.println("Third()"); } protected void finalize() throws Throwable { System.out.println("Third finalized()!"); if(DoBaseFinalization.flag) super.finalize(); } }
public class C07_11 { public static void main(String[] args) { if((args.length!=0)&&(args[0].equals("finalize"))) DoBaseFinalization.flag=true; else System.out.println("Not Finalized!"); //new Third(); System.out.println("Bye!"); System.gc(); } }
这是一个子类都能正确呼叫base class的finalize的例子,上面这段标记的地方如果用Third td=new Third(),那么就算引数输入finalize,也不能呼叫finalize的方法,一定要使用new Third(),我不知道为什么?请各位高手赐教阿!!!
|