Re: [心得] 序列化的小細節
問題出在這:(節錄自J2SE1.4 API)
The default serialization mechanism for an object writes the class of the
object, the class signature, and the values of all non-transient and non-static
fields. References to other objects (except in transient or static fields)
cause those objects to be written also. Multiple references to a single object
are encoded using a reference sharing mechanism so that graphs of objects can
be restored to the same shape as when the original was written.
所以"a.object"儲存了兩個物件, "b.object"也同樣儲存了兩個物件
(不信的話可以直接把a.object檔打開看, 一定看的懂的...)
因此讀取的結果是, A有兩個instances, B也有兩個
其中某個A.b是指向一個B instance; 另一個A的refernce b則是指向另一個B instance
自己畫畫圖就很清楚了...
不過也很感謝p大找出這個問題呢:)
※ 引述《PsMonkey (痞子軍團團長)》之銘言:
: 請原諒我幫忙加個註解...
: (坦白說,我翻來翻去,看好久才知道確定差異在哪 XDXD)
: ※ 引述《pao0111 (Pao)》之銘言:
: : 分別寫入兩個檔案:
: //這是會導致 reference 失敗的寫法
: //差異點在於,這邊存在兩個不同的檔案當中
: : FileOutputStream fos = new FileOutputStream("a.object");
: : ObjectOutputStream oos = new ObjectOutputStream(fos);
: : oos.writeObject(a);
: : oos.close();
: : fos = new FileOutputStream("b.object");
: : oos = new ObjectOutputStream(fos);
: : oos.writeObject(b);
: : oos.close();
: : 嗯,沒錯誤發生。
: : 可見互相參考甚至是循環參考對物件的序列化是沒問題的。
: : 在別的程式裡,讀出那兩個物件來:
: : FileInputStream fis = new FileInputStream("a.object");
: : ObjectInputStream ois = new ObjectInputStream(fis);
: : A a = (A)ois.readObject();
: : fis = new FileInputStream("b.object");
: : ois = new ObjectInputStream(fis);
: : B b = (B)ois.readObject();
: : 此時:
: : System.out.println(a.b == b);
: : System.out.println(a.b.a == a);
: : System.out.println(b.a == a);
: : System.out.println(b.a.b == b);
: : 印出來的是:
: : false
: : true
: : false
: : true
: : 哎呀!參照居然變了!
: 我比較想知道,這時候的 a.b 會指到哪裡去 @__@???
: 為甚麼 a.b.a 又會指回來自己 @__@???
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.139.216.85
※ 編輯: NTUtzboy 來自: 220.139.216.85 (10/26 01:38)
討論串 (同標題文章)