關於這個問題,寫了個簡單的代碼測試了下:
可序列化的類Father java
package com.taobao.test; import java.io.Serializable; /** * @author tina.wyn * */ public class Father implements Serializable { private static final long serialVersionUID = 1L; private Integer fatherage; private String str; public Father() { fatherage = 50; str = "I am father"; } public Integer getNumber() { return fatherage; } public void setNumber(Integer number) { this.fatherage = number; } public String getStr() { return str; } public void setStr(String str) { this.str = str; } }
不可序列化的類Mother 測試
package com.taobao.test; public class Mother { private Integer matherage; private String str; public Mother(){ matherage = 30; str = "I am mother"; } public Integer getMatherage() { return matherage; } public void setMatherage(Integer matherage) { this.matherage = matherage; } public String getStr() { return str; } public void setStr(String str) { this.str = str; } }
子類Son繼承了父類Father this
package com.taobao.test; /** * @author tina.wyn * */ public class Son extends Father { private static final long serialVersionUID = 1L; private int age; private transient String unserialize; public Son() { age = 10; unserialize = "hi"; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getUnserialize() { return unserialize; } public void setUnserialize(String unserialize) { this.unserialize = unserialize; } }
package com.taobao.test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; /** * @author tina.wyn * */ public class TestSerialize { public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { Son s = new Son(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("hello"))); objectOutputStream.writeObject(s); ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(new File("hello"))); Son sn = (Son) objectInputStream.readObject(); System.out.println(sn.getAge()); System.out.println(sn.getNumber()); System.out.println(sn.getStr()); System.out.println(sn.getUnserialize()); } }
運行結果 spa
10 50 I am father null
由此證實父類可序列化子類也可序列化,而sn.getUnserialize()爲null是由於子類裏將它定義爲了transient 類型的,因此子類的屬性能否序列化取決於如何定義。
如今咱們在子類里加入Mother類的引用 code
package com.taobao.test; /** * @author tina.wyn * */ public class Son extends Father { private static final long serialVersionUID = 1L; private int age; private transient String unserialize; Mother mother; public Son() { mother = new Mother(); age = 10; unserialize = "hi"; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getUnserialize() { return unserialize; } public void setUnserialize(String unserialize) { this.unserialize = unserialize; } public Mother getMother() { return mother; } public void setMother(Mother mother) { this.mother = mother; } }
運行結果: 繼承
Exception in thread "main" java.io.NotSerializableException: com.taobao.test.Mother at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164) at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518) at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483) at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400) at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330) at com.taobao.test.TestSerialize.main(TestSerialize.java:21)
吼吼,出錯鳥~Mother類不支持序列化哈~~~get