Java资源网

| JAVA基础 | 环境配置 | JDBC | 线程技术 | Socket编程 | JavaMail | JAVA与XML | 设计模式 | 技术新闻 | Java认证 | 程序人生 软件下载
| JSP&Servlet | Spring | Struts | Hibernate | JBuilder | Eclipse | WebService | EJB技术 | J2ME开发 | 应用服务器 | JXTA | Ajax
Articles search文章搜索
   关键字:
   类 别:
       
New download 最新下载
· [组件]HTML Parser 1.5
· [教程]WebSphere Studio应用教程
· [组件]JDom 1.0
· [工具]Junit3.8.1
· [教程]EJB编程及J2EE系统架构和设计
· [教程]EJB教程
· [教程]J2EE Tutorial中文版
· [教程]Java编程思想2(英文)
· [教程]java编程思想(完整版)
· [教程]Java网络编程
New articles 最新文章
· 设计移动 Web 服务
· 解析XML的时候完全忽略DTD
· 理解XML Schema XML Schema 初步
· 标签库的深入研究
· 提升JSP应用程序的七大绝招
· 如何使用JDOM对XML文件进行操作
· 处理XML字符串中特殊字符
· 利用Digester把XML转换成为Java对象
· 使用WebService 和RMI远程协作
· 使用Axis开发Web Service程序
Articles top 热门文章
· Eclipse基础--plugin插件安装(6644)
· eclipse+tomcat+lomboz的安装配置说明(4774)
· Java程序员就业前景(4584)
· Windows下JAVA环境变量的设置祥解(3788)
· Tomcat下JSP、Servlet和JavaBean环境的配置(3716)
· 使用links方式安装Eclipse插件(3698)
· 一个老程序员的心理话(3533)
· linux下jdk的安装与配置(3459)
· 初学者入门:Structs中基本配置入门(3334)
· Eclipse 运行命令行参数大全(3084)
您的位置:首页>>JAVA基础>>谈谈J2SE中的序列化之当序列化遭遇继承
谈谈J2SE中的序列化之当序列化遭遇继承
2005-08-23   来源:j2me开发网  作者:j2me开发网
  当一个父类实现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.<init>(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, 子类要先序列化自身,然后子类要负责序列化父类的域
  --相关文章--
· J2EE全面介绍(二) (2007-04-13)
· 项目经验二则:读取war包中的文件及Ant使用中的OutOfMemoryError解决 (2007-04-13)
· 走向J2EE,漫长的道路 (2007-04-13)
· 步入J2EE架构和过程(2) (2007-04-13)
· 步入J2EE架构和过程(1) (2007-04-13)
· 方兴未艾的CORBA (2007-04-13)

版权所有©2005-2006 JAVA资源网 渝ICP备05007591号 虚拟主机 | 关于我们 | 联系方式 | 广告业务 | 网站地图 | 友情链接