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

EXIAOMEI1

EXIAOMEI1

2016-02-19 13:44

有一种朋友不在生活里,却在生命力;有一种陪伴不在身边,却在心间。图老师即在大家的生活中又在身边。这么贴心的服务你感受到了吗?话不多说下面就和大家分享谈谈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.init(SubC.java:15)
  at Serial.Test1.main(Test1.java:19)
  Exception in thread "main"
  
展开更多 50%)
分享

猜你喜欢

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

编程语言 网络编程
谈谈J2SE中的序列化之当序列化遭遇继承

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

Java JAVA基础
谈谈J2SE中当序列化遭遇继承

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

谈谈J2SE中的序列化之接受默认序列化

编程语言 网络编程
谈谈J2SE中的序列化之接受默认序列化

J2SE中的序列化之继承

Java JAVA基础
J2SE中的序列化之继承

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

J2SE中的序列化之接受默认序列化

编程语言 网络编程
J2SE中的序列化之接受默认序列化

J2SE中的序列化的认识

Java JAVA基础
J2SE中的序列化的认识

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

Excel 2010中撤消、恢复及重复操作

Excel 2010中撤消、恢复及重复操作

Win7快捷键学问大 丢进回收站Ctrl+D键搞定

Win7快捷键学问大 丢进回收站Ctrl+D键搞定
下拉加载更多内容 ↓