父類實現了Serializable,子類不需要實現Serializable
相關注意事項
a)序列化時,只對對象的狀態進行保存,而不管對象的方法;
b)當一個父類實現序列化,子類自動實現序列化,不需要顯式實現Serializable接口;
c)當一個對象的實例變量引用其他對象,序列化該對象時也把引用對象進行序列化;
d)並非所有的對象都可以序列化,至於為什么不可以,有很多原因了,比如:
1.安全方面的原因,比如一個對象擁有private,public等field,對於一個要傳輸的對象,比如寫到文件,或者進行rmi傳輸等等,在序列化進行傳輸的過程中,這個對象的private等域是不受保護的。
2. 資源分配方面的原因,比如socket,thread類,如果可以序列化,進行傳輸或者保存,也無法對他們進行重新的資源分配,而且,也是沒有必要這樣實現。
例子:
1,父類實現了Serializable,子類沒有,
父類有int a = 1int b = 2int c = 3
子類有int d = 4int e = 5
序列化子類的時候,d和e會不會被序列化?(答案:會)
2,反過來父類未實現Serializable,子類實現了,序列化子類實例的時候,父類的屬性是直接被跳過不保存,還是能保存但不能還原?(答案:值不保存)
解:父類實現接口后,所有派生類的屬性都會被序列化。子類實現接口的話,父類的屬性值丟失。
java中序列化之子類繼承父類序列化
當一個父類實現Serializable接口后,他的子類都將自動的實現序列化。
以下驗證了這一點:
package Serial;
import java.io.Serializable;
public class SuperC implements Serializable {//父類實現了序列化
int supervalue;
public SuperC(int supervalue) {
this.supervalue = supervalue;
}
public String toString() {
return "supervalue: "+supervalue;
}
}
public class SubC extends SuperC {//子類
int subvalue;
public SubC(int supervalue,int subvalue) {
super(supervalue);
this.subvalue=subvalue;
}
public String toString() {
return super.toString()+" sub: "+subvalue;
}
}
public class Test1 {
public static void main(String [] args){
SubC subc=new SubC(100,200);
FileInputStream in=null;
FileOutputStream out=null;
ObjectInputStream oin=null;
ObjectOutputStream oout=null;
try {
out = new FileOutputStream("Test1.txt");//子類序列化
oout = new ObjectOutputStream(out);
oout.writeObject(subc);
oout.close();
oout=null;
in = new FileInputStream("Test1.txt");
oin = new ObjectInputStream(in);
SubC subc2=(SubC)oin.readObject();//子類反序列化
System.out.println(subc2);
} catch (Exception ex){
ex.printStackTrace();
} finally{
…此處省略
}
}
}
運行結果如下:
supervalue: 100 sub: 200
可見子類成功的序列化/反序列化了。
怎管讓子類實現序列化看起來是一件很簡單的事情,但有的時候,往往我們不能夠讓父類實現Serializable接口,原因是有時候父類是抽象的(這並沒有關系),並且父類不能夠強制每個子類都擁有序列化的能力。換句話說父類設計的目的僅僅是為了被繼承。
要為一個沒有實現Serializable接口的父類,編寫一個能夠序列化的子類是一件很麻煩的事情。java docs中提到:
“To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of the supertype's public, protected, and (if accessible) package fields. The subtype may assume this responsibility only if the class it extends has an accessible no-arg constructor to initialize the class's state. It is an error to declare a class Serializable if this is not the case. The error will be detected at runtime. ”
也就是說,要為一個沒有實現Serializable接口的父類,編寫一個能夠序列化的子類要做兩件事情:
其一、父類要有一個無參的constructor;
其二、子類要負責序列化(反序列化)父類的域。
我們將SuperC的Serializable接口去掉,而給SubC加上Serializable接口。運行后產生錯誤:
java.lang.Error: Unresolved compilation problem:
Serializable cannot be resolved or is not a valid superinterface
at Serial.SubC.<init>(SubC.java:15)
at Serial.Test1.main(Test1.java:19)
Exception in thread "main"
果真如docs中所說的一樣,父類缺少無參構造函數是不行的。
接下來,按照docs中的建議我們改寫這個例子:
public abstract class SuperC {
int supervalue;
public SuperC(int supervalue) {
this.supervalue = supervalue;
}
public SuperC(){}//增加一個無參的constructor
public String toString() {
return "supervalue: "+supervalue;
}
}
public class SubC extends SuperC implements Serializable {
int subvalue;
public SubC(int supervalue,int subvalue) {
super(supervalue);
this.subvalue=subvalue;
}
public String toString() {
return super.toString()+" sub: "+subvalue;
}
private void writeObject(java.io.ObjectOutputStream out)
throws IOException{
out.defaultWriteObject();//先序列化對象
out.writeInt(supervalue);//再序列化父類的域
}
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException{
in.defaultReadObject();//先反序列化對象
supervalue=in.readInt();//再反序列化父類的域
}
}
運行結果證明了這種方法是正確的。在此處我們用到了writeObject/ readObject方法,這對方法如果存在的話,序列化時就會被調用,以代替默認的行為(以后還要探討,先了解這么多)。我們在序列化時,首先調用了ObjectOutputStream的defaultWriteObject,它使用默認的序列化行為,然后序列化父類的域;反序列化的時候也一樣。
歸納一下:
目的 行為
為一個實現Serializable接口的父類,編寫一個能夠序列化的子類 子類將自動的實現序列化
為一個沒有實現Serializable接口的父類,編寫一個能夠序列化的子類 1, 父類要有一個無參的constructor;2, 子類要先序列化自身,然后子類要負責序列化父類的域
引用:http://www.yesky.com/376/1908876.shtml
跟多參考:http://www.ibm.com/developerworks/cn/java/j-lo-serial/