J2SE中的序列化之繼承
更新時(shí)間:2006年10月13日 00:00:00 作者:
當(dāng)一個(gè)父類(lèi)實(shí)現(xiàn)Serializable接口后,他的子類(lèi)都將自動(dòng)的實(shí)現(xiàn)序列化。
以下驗(yàn)證了這一點(diǎn):
package Serial;
import java.io.Serializable;
public class SuperC implements Serializable {//父類(lèi)實(shí)現(xiàn)了序列化
int supervalue;
public SuperC(int supervalue) {
this.supervalue = supervalue;
}
public String toString() {
return "supervalue: "+supervalue;
}
}
public class SubC extends SuperC {//子類(lèi)
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");//子類(lèi)序列化
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();//子類(lèi)反序列化
System.out.println(subc2);
} catch (Exception ex){
ex.printStackTrace();
} finally{
…此處省略
}
}
}
運(yùn)行結(jié)果如下:
supervalue: 100 sub: 200
可見(jiàn)子類(lèi)成功的序列化/反序列化了。
怎管讓子類(lèi)實(shí)現(xiàn)序列化看起來(lái)是一件很簡(jiǎn)單的事情,但有的時(shí)候,往往我們不能夠讓父類(lèi)實(shí)現(xiàn)Serializable接口,原因是有時(shí)候父類(lèi)是抽象的(這并沒(méi)有關(guān)系),并且父類(lèi)不能夠強(qiáng)制每個(gè)子類(lèi)都擁有序列化的能力。換句話說(shuō)父類(lèi)設(shè)計(jì)的目的僅僅是為了被繼承。
要為一個(gè)沒(méi)有實(shí)現(xiàn)Serializable接口的父類(lèi),編寫(xiě)一個(gè)能夠序列化的子類(lèi)是一件很麻煩的事情。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. ”
也就是說(shuō),要為一個(gè)沒(méi)有實(shí)現(xiàn)Serializable接口的父類(lèi),編寫(xiě)一個(gè)能夠序列化的子類(lèi)要做兩件事情:
其一、父類(lèi)要有一個(gè)無(wú)參的constructor;
其二、子類(lèi)要負(fù)責(zé)序列化(反序列化)父類(lèi)的域。
我們將SuperC的Serializable接口去掉,而給SubC加上Serializable接口。運(yùn)行后產(chǎn)生錯(cuò)誤:
java.lang.Error: Unresolved compilation problem:
Serializable cannot be resolved or is not a valid superinterface
at Serial.SubC.(SubC.java:15)
at Serial.Test1.main(Test1.java:19)
Exception in thread "main"
果真如docs中所說(shuō)的一樣,父類(lèi)缺少無(wú)參構(gòu)造函數(shù)是不行的。
接下來(lái),按照docs中的建議我們改寫(xiě)這個(gè)例子:
public abstract class SuperC {
int supervalue;
public SuperC(int supervalue) {
this.supervalue = supervalue;
}
public SuperC(){}//增加一個(gè)無(wú)參的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();//先序列化對(duì)象
out.writeInt(supervalue);//再序列化父類(lèi)的域
}
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException{
in.defaultReadObject();//先反序列化對(duì)象
supervalue=in.readInt();//再反序列化父類(lèi)的域
}
}
運(yùn)行結(jié)果證明了這種方法是正確的。在此處我們用到了writeObject/ readObject方法,這對(duì)方法如果存在的話,序列化時(shí)就會(huì)被調(diào)用,以代替默認(rèn)的行為(以后還要探討,先了解這么多)。我們?cè)谛蛄谢瘯r(shí),首先調(diào)用了ObjectOutputStream的defaultWriteObject,它使用默認(rèn)的序列化行為,然后序列化父類(lèi)的域;反序列化的時(shí)候也一樣。
歸納一下:
目的 行為
為一個(gè)實(shí)現(xiàn)Serializable接口的父類(lèi),編寫(xiě)一個(gè)能夠序列化的子類(lèi) 子類(lèi)將自動(dòng)的實(shí)現(xiàn)序列化
為一個(gè)沒(méi)有實(shí)現(xiàn)Serializable接口的父類(lèi),編寫(xiě)一個(gè)能夠序列化的子類(lèi) 1, 父類(lèi)要有一個(gè)無(wú)參的constructor;2, 子類(lèi)要先序列化自身,然后子類(lèi)要負(fù)責(zé)序列化父類(lèi)的域
以下驗(yàn)證了這一點(diǎn):
package Serial;
import java.io.Serializable;
public class SuperC implements Serializable {//父類(lèi)實(shí)現(xiàn)了序列化
int supervalue;
public SuperC(int supervalue) {
this.supervalue = supervalue;
}
public String toString() {
return "supervalue: "+supervalue;
}
}
public class SubC extends SuperC {//子類(lèi)
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");//子類(lèi)序列化
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();//子類(lèi)反序列化
System.out.println(subc2);
} catch (Exception ex){
ex.printStackTrace();
} finally{
…此處省略
}
}
}
運(yùn)行結(jié)果如下:
supervalue: 100 sub: 200
可見(jiàn)子類(lèi)成功的序列化/反序列化了。
怎管讓子類(lèi)實(shí)現(xiàn)序列化看起來(lái)是一件很簡(jiǎn)單的事情,但有的時(shí)候,往往我們不能夠讓父類(lèi)實(shí)現(xiàn)Serializable接口,原因是有時(shí)候父類(lèi)是抽象的(這并沒(méi)有關(guān)系),并且父類(lèi)不能夠強(qiáng)制每個(gè)子類(lèi)都擁有序列化的能力。換句話說(shuō)父類(lèi)設(shè)計(jì)的目的僅僅是為了被繼承。
要為一個(gè)沒(méi)有實(shí)現(xiàn)Serializable接口的父類(lèi),編寫(xiě)一個(gè)能夠序列化的子類(lèi)是一件很麻煩的事情。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. ”
也就是說(shuō),要為一個(gè)沒(méi)有實(shí)現(xiàn)Serializable接口的父類(lèi),編寫(xiě)一個(gè)能夠序列化的子類(lèi)要做兩件事情:
其一、父類(lèi)要有一個(gè)無(wú)參的constructor;
其二、子類(lèi)要負(fù)責(zé)序列化(反序列化)父類(lèi)的域。
我們將SuperC的Serializable接口去掉,而給SubC加上Serializable接口。運(yùn)行后產(chǎn)生錯(cuò)誤:
java.lang.Error: Unresolved compilation problem:
Serializable cannot be resolved or is not a valid superinterface
at Serial.SubC.
at Serial.Test1.main(Test1.java:19)
Exception in thread "main"
果真如docs中所說(shuō)的一樣,父類(lèi)缺少無(wú)參構(gòu)造函數(shù)是不行的。
接下來(lái),按照docs中的建議我們改寫(xiě)這個(gè)例子:
public abstract class SuperC {
int supervalue;
public SuperC(int supervalue) {
this.supervalue = supervalue;
}
public SuperC(){}//增加一個(gè)無(wú)參的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();//先序列化對(duì)象
out.writeInt(supervalue);//再序列化父類(lèi)的域
}
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException{
in.defaultReadObject();//先反序列化對(duì)象
supervalue=in.readInt();//再反序列化父類(lèi)的域
}
}
運(yùn)行結(jié)果證明了這種方法是正確的。在此處我們用到了writeObject/ readObject方法,這對(duì)方法如果存在的話,序列化時(shí)就會(huì)被調(diào)用,以代替默認(rèn)的行為(以后還要探討,先了解這么多)。我們?cè)谛蛄谢瘯r(shí),首先調(diào)用了ObjectOutputStream的defaultWriteObject,它使用默認(rèn)的序列化行為,然后序列化父類(lèi)的域;反序列化的時(shí)候也一樣。
歸納一下:
目的 行為
為一個(gè)實(shí)現(xiàn)Serializable接口的父類(lèi),編寫(xiě)一個(gè)能夠序列化的子類(lèi) 子類(lèi)將自動(dòng)的實(shí)現(xiàn)序列化
為一個(gè)沒(méi)有實(shí)現(xiàn)Serializable接口的父類(lèi),編寫(xiě)一個(gè)能夠序列化的子類(lèi) 1, 父類(lèi)要有一個(gè)無(wú)參的constructor;2, 子類(lèi)要先序列化自身,然后子類(lèi)要負(fù)責(zé)序列化父類(lèi)的域
相關(guān)文章
快速建立Servlet和JSP的運(yùn)行、調(diào)試和編譯環(huán)境
快速建立Servlet和JSP的運(yùn)行、調(diào)試和編譯環(huán)境...2006-10-10
JSP MySQL插入數(shù)據(jù)時(shí)出現(xiàn)中文亂碼問(wèn)題的解決方法
這篇文章主要介紹了JSP MySQL插入數(shù)據(jù)時(shí)出現(xiàn)中文亂碼問(wèn)題的解決方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
用連接池提高Servlet訪問(wèn)數(shù)據(jù)庫(kù)的效率(1)
用連接池提高Servlet訪問(wèn)數(shù)據(jù)庫(kù)的效率(1)...2006-10-10
實(shí)戰(zhàn) J2EE 開(kāi)發(fā)購(gòu)物網(wǎng)站 二
實(shí)戰(zhàn) J2EE 開(kāi)發(fā)購(gòu)物網(wǎng)站 二...2006-10-10
JSP上傳excel及excel插入至數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了JSP上傳excel及excel插入至數(shù)據(jù)庫(kù)的方法,涉及JSP文件上傳及針對(duì)excel的讀取、寫(xiě)入數(shù)據(jù)庫(kù)等操作技巧,需要的朋友可以參考下2015-10-10
JBuilder2005單元測(cè)試之業(yè)務(wù)類(lèi)介紹
JBuilder2005單元測(cè)試之業(yè)務(wù)類(lèi)介紹...2006-10-10

