7.3 超类具体化的具体化
当用具体化接口时,一个具体化对象必须运行writeExternal()方法存储对象的状态,用readExternal方法读取对象的状态。此例子验证了一个怎样存储和恢复它可具体化超类对象的状态。当一个可具体化对象的超类也具体化,子类要在它自己的writeExternal()和readExternal()方法中调用其超类的writeExternal()和readExternal()方法。
|
/*SaveSuper*/ package com.asc.alibaba.base;
import Java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream;
publicclass SaveSuper {
publicstaticvoid main(String args[]) { Book1 bookorg = new Book1(100, "How to Serialize", true, "R.R", "Serialization", 97); Book1 booknew = null; try { FileOutputStream fo = new FileOutputStream("tmp"); ObjectOutputStream so = new ObjectOutputStream(fo); so.writeObject(bookorg); so.flush(); } catch (Exception e) { System.out.println(e); System.exit(1); } try { FileInputStream fi = new FileInputStream("tmp"); ObjectInputStream si = new ObjectInputStream(fi); booknew = (Book1) si.readObject(); } catch (Exception e) { System.out.println(e); System.exit(1); } System.out.println(); System.out.println("Printing original book.."); System.out.println(bookorg); System.out.println("Printing new book.."); System.out.println(booknew); System.out.println("Both original and new should be the same"); System.out.println(); } } /*Book1*/ package com.asc.alibaba.base;
import java.io.Externalizable; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput;
class Book1 extends ReadingMaterial1 implements Externalizable {
privateint numpages; private String name; privatebooleanishardcover;
public Book1(){ super(); }
public Book1(int pages, String n, boolean hardcover, String author, String subject, int yearwritten){ super(author, subject, yearwritten); numpages = pages; name = n; ishardcover = hardcover; }
publicvoid witeExternal(ObjectOutput out) throws IOException { super.writeExternal(out); }
publicvoid readExternal(ObjectInput in) throws IOException, ClassNotFoundException { super.readExternal(in); numpages = in.readInt(); name = (String) in.readObject(); ishardcover = in.readBoolean(); }
public String toString() { return ("Name:" + name + "\n" + "Author:" + super.getAuthor() + "\n" + "Pages:" + numpages + "\n" + "Subject:" + super.getSubject() + "\n" + "Year:" + super.getYearwritten() + "\n"); } }
/*ReadingMaterial1*/ package com.asc.alibaba.base;
import java.io.Externalizable; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput;
class ReadingMaterial1 implements Externalizable {
private String author; private String subject; privateint yearwritten;
public ReadingMaterial1(){ }
public ReadingMaterial1(String auth, String sub, int year){ author = auth; subject = sub; yearwritten = year; }
public String getAuthor() { returnauthor; }
public String getSubject() { returnsubject; }
publicint getYearwritten() { returnyearwritten; }
publicvoid writeExternal(ObjectOutput out) throws IOException { out.writeObject(author); out.writeObject(subject); out.writeInt(yearwritten); }
publicvoid readExternal(ObjectInput in) throws IOException, ClassNotFoundException { author = (String) in.readObject(); subject = (String) in.readObject(); yearwritten = in.readInt(); } } 结果如下: Printing original book.. Name:How to Serialize Author:R.R Pages:100 Subject:Serialization Year:97
Name:null Author:R.R Pages:0 Subject:Serialization Year:97 Both original and new should be the same |
7.4 超类非具体化的具体化
当用具体化接口时,一个具体化对象必须运行writeExternal()方法存储对象的状态,用readExternal()方法读取对象的状态。此例子验证了一个对象怎样存储和恢复它非具体化超类的状态。当一个可具体化对象的超类没有具体化,子类必须用它自己的writeExternal()和readExternal()方法明确存储和恢复其超类的可具体化对象状态。
|
/*Nonexternsuper1*/ package com.asc.alibaba.base;
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream;
publicclass Nonexternsuper1 {
publicstaticvoid main(String args[]) { Book2 bookorg = new Book2(100, "How to Serialize", true, "R.R", "Serialization", 97); Book2 booknew = null; try { FileOutputStream fo = new FileOutputStream("tmp"); ObjectOutputStream so = new ObjectOutputStream(fo); so.writeObject(bookorg); so.flush(); } catch (Exception e) { System.out.println(e); System.exit(1); } try { FileInputStream fi = new FileInputStream("tmp"); ObjectInputStream si = new ObjectInputStream(fi); booknew = (Book2) si.readObject(); } catch (Exception e) { System.out.println(e); System.exit(1); } System.out.println(); System.out.println("Printing original book.."); System.out.println(bookorg); System.out.println("Printing new book.."); System.out.println(booknew); System.out.println("Both original and new should be the same"); System.out.println(); } } /*Book2*/ package com.asc.alibaba.base;
import java.io.Externalizable; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput;
class Book2 extends ReadingMaterial2 implements Externalizable {
int numpages; String name; booleanishardcover;
public Book2(){ super(); }
Book2(int pages, String n, boolean hardcover, String author, String subject, int yearwritten){ super(author, subject, yearwritten); numpages = pages; name = n; ishardcover = hardcover; }
publicvoid writeExternal(ObjectOutput out) throws IOException { out.writeObject(author); out.writeObject(subject); out.writeInt(yearwritten); out.writeInt(numpages); out.writeObject(name); out.writeBoolean(ishardcover); }
publicvoid readExternal(ObjectInput in) throws IOException, ClassNotFoundException { author = (String) in.readObject(); subject = (String) in.readObject(); yearwritten = in.readInt(); numpages = in.readInt(); name = (String) in.readObject(); ishardcover = in.readBoolean(); }
public String toString() { return ("Name:" + name + "\n" + "Author:" + author + "\n" + "Pages:" + numpages + "\n" + "Subject:" + subject + "\n" + "Year:" + yearwritten + "\n"); } } /*ReadingMaterial2*/ package com.asc.alibaba.base;
publicclass ReadingMaterial2 {
String author; String subject; int yearwritten;
public ReadingMaterial2(){ }
ReadingMaterial2(String auth, String sub, int year){ author = auth; subject = sub; yearwritten = year; } } 结果: Printing original book.. Name:How to Serialize Author:R.R Pages:100 Subject:Serialization Year:97
Printing new book.. Name:How to Serialize Author:R.R Pages:100 Subject:Serialization Year:97
Both original and new should be the same |
