网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.使用JWSDP完成Web Service在java.
.JavaCard开发教程之规范.
.GreedySnake贪吃蛇-测试版.
.Java 概 述.
.Java手机软件图形界面API之低级G.
.Jive笔记--DatabasePackage下面的.
.浅析Java多线程编程中的高级技术.
.java入门文章--安装.
.Canvas中使用Font.
.使用Quick在Java对象和XML之间进.
.JBuilder开发常用的十九个快捷键.
.Java Socket网络编程初级入.
.开源技术:tomcat5.0下配置ssl.
.使用Eclipse Callisto分析应用程.
.hibernate应用配置说明-middlege.
.Eclipse快速上手指南之使用JUnit.
.使用Java Swing 创建一个XML编辑.
.java读文件.
.J2SE 1.5新特性简介.
.Java新手上路大全.

谈谈J2SE中的序列化之当序列化遭遇继承

发表日期:2008-1-5


  当一个父类实现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"

上一篇:JBuilder和JDeveloper的简单比较 人气:574
下一篇:用简单的J2ME程序测试MIDlet生命周期 人气:603
浏览全部Java的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐