大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> JSP专区 -> J2SE中的序列化之继承

J2SE中的序列化之继承

时间: 2021-07-31 作者:daque

当一个父类实行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, 子类要先序列化自己,而后子类要控制序列化父类的域

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.