婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

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

J2SE中的序列化之繼承

熱門標簽:商丘百應電話機器人有沒有效果 淮南騰訊地圖標注 電話機器人的特色和創新 開封便宜外呼系統報價 地圖標注人員兼職 騰訊地圖標注商戶改名注冊入駐 怎樣把地圖標注出來 漯河辦理400電話 黃石智能營銷電銷機器人效果
當一個父類實現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.(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, 子類要先序列化自身,然后子類要負責序列化父類的域

標簽:鄭州 紅河 拉薩 馬鞍山 岳陽 大興安嶺 武威 亳州

巨人網絡通訊聲明:本文標題《J2SE中的序列化之繼承》,本文關鍵詞  J2SE,中的,序列化,之,繼承,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《J2SE中的序列化之繼承》相關的同類信息!
  • 本頁收集關于J2SE中的序列化之繼承的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 定陶县| 融水| 寿光市| 郯城县| 三明市| 鸡东县| 扎囊县| 延吉市| 喀喇| 洪泽县| 永德县| 浦县| 原阳县| 衡水市| 新竹市| 广饶县| 班玛县| 永登县| 萍乡市| 嘉峪关市| 赞皇县| 邯郸市| 汽车| 寿宁县| 宁波市| 宝丰县| 桂阳县| 华池县| 昭觉县| 陕西省| 西畴县| 铁岭县| 山阳县| 陕西省| 榆林市| 通江县| 上饶县| 高州市| 天水市| 新密市| 赤城县|