佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > J2SE中的序列化之繼承

J2SE中的序列化之繼承

熱門標(biāo)簽:團(tuán)購網(wǎng)站 Linux服務(wù)器 銀行業(yè)務(wù) 電子圍欄 阿里云 Mysql連接數(shù)設(shè)置 科大訊飛語音識別系統(tǒng) 服務(wù)器配置
當(dāng)一個父類實(shí)現(xiàn)Serializable接口后,他的子類都將自動的實(shí)現(xiàn)序列化。

  以下驗(yàn)證了這一點(diǎn):

  package Serial;
  import java.io.Serializable;
  public class SuperC implements Serializable {//父類實(shí)現(xiàn)了序列化
  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{
  …此處省略
  }
  }
  }

  運(yùn)行結(jié)果如下:

  supervalue: 100 sub: 200

  可見子類成功的序列化/反序列化了。

  怎管讓子類實(shí)現(xiàn)序列化看起來是一件很簡單的事情,但有的時候,往往我們不能夠讓父類實(shí)現(xiàn)Serializable接口,原因是有時候父類是抽象的(這并沒有關(guān)系),并且父類不能夠強(qiáng)制每個子類都擁有序列化的能力。換句話說父類設(shè)計(jì)的目的僅僅是為了被繼承。

  要為一個沒有實(shí)現(xiàn)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. ”

  也就是說,要為一個沒有實(shí)現(xiàn)Serializable接口的父類,編寫一個能夠序列化的子類要做兩件事情:

  其一、父類要有一個無參的constructor;

  其二、子類要負(fù)責(zé)序列化(反序列化)父類的域。

  我們將SuperC的Serializable接口去掉,而給SubC加上Serializable接口。運(yùn)行后產(chǎn)生錯誤:

  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中所說的一樣,父類缺少無參構(gòu)造函數(shù)是不行的。

  接下來,按照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();//再反序列化父類的域
  }
  }

  運(yùn)行結(jié)果證明了這種方法是正確的。在此處我們用到了writeObject/ readObject方法,這對方法如果存在的話,序列化時就會被調(diào)用,以代替默認(rèn)的行為(以后還要探討,先了解這么多)。我們在序列化時,首先調(diào)用了ObjectOutputStream的defaultWriteObject,它使用默認(rèn)的序列化行為,然后序列化父類的域;反序列化的時候也一樣。

  歸納一下:

  目的 行為

  為一個實(shí)現(xiàn)Serializable接口的父類,編寫一個能夠序列化的子類 子類將自動的實(shí)現(xiàn)序列化

  為一個沒有實(shí)現(xiàn)Serializable接口的父類,編寫一個能夠序列化的子類 1, 父類要有一個無參的constructor;2, 子類要先序列化自身,然后子類要負(fù)責(zé)序列化父類的域

標(biāo)簽:大理 廣元 江蘇 棗莊 萍鄉(xiāng) 蚌埠 衡水 衢州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《J2SE中的序列化之繼承》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    无锡市| 沂源县| 海城市| 公安县| 古田县| 驻马店市| 朝阳市| 深泽县| 兴宁市| 华阴市| 井冈山市| 鄂托克旗| 南阳市| 合水县| 财经| 临猗县| 湖北省| 盈江县| 江都市| 汨罗市| 礼泉县| 漾濞| 和田市| 通州区| 陈巴尔虎旗| 东乡县| 象山县| 沽源县| 若尔盖县| 桂阳县| 惠安县| 澄城县| 安宁市| 隆回县| 金山区| 久治县| 宝山区| 芦山县| 兰西县| 淮南市| 濮阳市|