繼承和finalize()的問題

看板java作者時間19年前 (2006/02/25 20:01), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
以下是Thinking in Java中譯本當中P333~335的code class DoBaseFinalization{ public static boolean flag=false; } class Characteristic{ String s; Characteristic(String c){ s=c; System.out.println("Creating Characteristic "+s); } protected void finalize(){ System.out.println("finalizing Characteristic "+s); } } class LivingCreature{ Characteristic p =new Characteristic("is alive"); LivingCreature(){ System.out.println("LivingCreature()"); } protected void finalize() throws Throwable{ System.out.println("LivingCreature finalize"); if(DoBaseFinalization.flag) super.finalize(); } } class Animal extends LivingCreature{ Characteristic p =new Characteristic("hac heart"); Animal(){ System.out.println("Anaimal()"); } protected void finalize() throws Throwable{ System.out.println("Animal finalize"); if(DoBaseFinalization.flag) super.finalize(); } } class Amphibian extends Animal{ Characteristic p =new Characteristic("can live in water"); Amphibian(){ System.out.println("Amphibian()"); } protected void finalize() throws Throwable{ System.out.println("Amphibian finalize"); if (DoBaseFinalization.flag) super.finalize(); } } public class Frog extends Amphibian { Frog(){ System.out.println("Frog()"); } protected void finalize() throws Throwable{ System.out.println("Frog finalize"); if(DoBaseFinalization.flag) super.finalize(); } public static void main(String[] args) { if(args.length !=0 && args[0].equals("finalize")) DoBaseFinalization.flag=true; else System.out.println("Not finalizing bases"); new Frog(); System.out.println("Bye!!"); System.gc(); } } 然後執行所得的結果如下: Not finalizing bases Creating Characteristic is alive LivingCreature() Creating Characteristic has heart Animal() Creating Characteristic can live in water Amphibian() Frog() Bye! finalizing Characteristic can live in water finalizing Characteristic has heart finalizing Characterisitc is alive Frog finalize 在此提出幾個問題,多謝指導!! 1.System.gc之後的動作會呼叫finalize(),但帶有extends的關係中,順序是什麼呢? 2.在該本書中的執行結果為,請問是不是有誤呢? (上面略) Bye! Frog finalize finalizing Characterisitc is alive finalizing Characteristic has heart finalizing Characteristic can live in water -- 夫兵者不祥之器物或惡之故有道者不處君子居則貴左用兵則貴右兵者不祥之器非君子 之器不得已而用之恬淡為上勝而不美而美之者是樂殺人夫樂殺人者則不可得志於天下 矣吉事尚左凶事尚右偏將軍居左上將軍居右言以喪禮處之殺人之眾以哀悲泣之戰勝以 喪禮處之道常無名樸雖小天下莫能臣侯王若能守之萬物將自賓天地相合以降甘露民莫 之令而自均始制有名名亦既有夫亦將知止知止可以不 h99-158.dorm1.ccit.edu.tw
文章代碼(AID): #1404S000 (java)
文章代碼(AID): #1404S000 (java)