推 Robert0512:感謝講解 是我弄錯了一些地方 囧 03/15 19:20
※ 引述《Robert0512 (天天衝浪三國無雙)》之銘言:
: 喔喔 我是把每個的finalize()都寫不同的
: 譬如C就是
: public void finalize(){
: System.out.printly("this is C");
: }
: 這樣會有問題出現嗎?
我做過實驗,只會出現 c 的 finalize call: (JDK 1.5)
a.java:
public class a {
public a() { System.out.println("instance of class a, created."); }
protected void finalize() throws Throwable {
System.out.println("instance of class a, finalized.");
}
}
b.java:
public class b extends a {
public b() { System.out.println("instance of class b, created."); }
protected void finalize() throws Throwable {
System.out.println("instance of class b, finalized.");
}
}
c.java:
public class c extends b {
public c() { System.out.println("instance of class c, created."); }
protected void finalize() throws Throwable {
System.out.println("instance of class c, finalized.");
}
public static void main(String arg[]) throws Exception {
c obj = new c();
obj = null;
System.gc();
Thread.sleep(6000000);
}
}
結果如下:
instance of class a, created.
instance of class b, created.
instance of class c, created.
instance of class c, finalized.
--
勁過呂布的勁過相簿...
http://www.pixnet.net/superlubu
亂七八糟的,不好意思 m(_ _)m
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.103.138.188
※ 編輯: superlubu 來自: 218.103.138.188 (03/15 19:12)