以下驗證了這一點:

  package Serial;   import java.io.Serializable;   public class SuperC implements Serializable {//父類實現 " /> 2020亚洲色噜噜狠狠网站,伊人久久电影院,青青青久草

天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

J2SE中的序列化之繼承

當一個父類實現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, 子類要先序列化自身,然后子類要負責序列化父類的域

jsp技術J2SE中的序列化之繼承,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 久久免费特黄毛片 | 国产成人在线播放 | 一级做a爰片久久毛片一 | 蜜桃TV成人网站免费打开 | 久久久国产精品免费A片3D | 久久综合九色 | 涩涩电影网 | 久久成人精品免费播放 | 国产精品自在在线午夜蜜芽tv在线 | 外国三级片名 | 中字幕视频在线永久在线观看免费 | 爆乳啪啪无码成人二区亚洲欧美 | 午夜一个人在线观看完整版 | 成人麻豆日韩在无码视频 | 双性精跪趴灌满h室友4p | 综合色就爱涩涩涩综合婷婷 | 九色PORNY真实丨首页 | 国产在线观看91精品 | 男女床上黄色 | 在线综合 亚洲 欧美 | 久久精品AV一区二区无码 | 久久精品伊人 | 性色香蕉AV久久久天天网 | 一起碰一起噜一起草视频 | qvod12| 精彩国产萝视频在线 | 青娱乐国产精品视频 | 野花日本大全免费高清完整版 | 色姊姊真舒服 | 青青草AV国产精品 | 亚洲精品高清在线观看 | 性夜a爽黄爽 | 99久久国产综合精品成人影院 | 超碰免费视频caopoom9 | 偷拍自偷拍亚洲精品 | 秋霞电影网视频一区二区三区 | 久草精品在线 | 婷婷六月激情综合一区 | www.97干| 精品久久久久久无码人妻国产馆 | 老太脱裤子让老头玩xxxxx |