繁体   English   中英

如果序列化是在层次结构中继承的,则将调用未实现可序列化的类的构造函数。 为什么会这样呢?

[英]If the serialization is inherited in a hierarchy then the constructor of class not implementing serializable will be called. Why is this so?

当我反序列化以下示例中的SkinFruit和Banana2的构造函数时,为什么要调用Food and Fruit类的构造函数?

假设我有以下课程层次结构

class Food{
  public Food() { System.out.println("1"); } 
    }

class Fruit extends Food {
    public Fruit() { System.out.print("2"); }
}

class SkinFruit extends Fruit implements Serializable{
    SkinFruit() { System.out.print("3"); }
}
public class Banana2 extends SkinFruit { 
  private static final long serialVersionUID = 1L;
  int size = 42;

     public static void main(String [] args) {
       Banana2 b = new Banana2();
       FileOutputStream fost;
        try {
            fost = new FileOutputStream(new File("d:/testbanana.ser"));
            ObjectOutputStream oostr= new ObjectOutputStream(fost);
            oostr.writeObject(b);
            oostr.flush();          
            oostr.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }

        try {
            FileInputStream fin= new FileInputStream(new File("d:/testbanana.ser"));
            ObjectInputStream objin= new ObjectInputStream(fin);
             b=(Banana2)objin.readObject();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


       System.out.println(" restored "+ b.size + "" );
     }
}

因为这就是反序列化的工作方式。 它是与常规对象构造不同的机制。

如果调用了Class A的构造函数,那是因为Class A本身未实现Serializable 反序列化期间,将调用继承层次结构中第一个不可序列化类的无参数构造函数(可能是java.lang.Object无参数构造函数)。 如果该类没有无参数的构造函数,则将发生异常。

两者都不被调用,因为您的类不可序列化。

您不能不调用B()就调用A()因为B调用了A。

考虑以下程序

public static void main(String... args) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(new B());
    oos.writeObject(new D());
    oos.close();
    System.out.println("-------------");
    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
    System.out.println("deserialize B");
    ois.readObject();
    System.out.println("deserialize D");
    ois.readObject();
}

static class A {
    public A() {
        System.out.println("Inside class A");
    }
}

static class B extends A  implements Serializable {
    public B() {
        System.out.println("Inside class B");
    }
}

static class C implements Serializable  {
    public C() {
        System.out.println("Inside class C");
    }
}

static class D extends C {
    public D() {
        System.out.println("Inside class D");
    }
}

版画

Inside class A
Inside class B
Inside class C
Inside class D
-------------
deserialize B
Inside class A
deserialize D

A是B的不可序列化的父级。它没有被序列化,因此必须调用构造函数以设置需要设置的任何字段。 但是,B,C和D都是可序列化的,并且它们的字段已被序列化并将被重置。 无需调用构造函数。

如果您要在反序列化对象时完全重新创建一些信息,则应在Serializable类中使用以下方法实现。 例如,将下面的代码放入B

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    System.out.println("Resurrecting B");
}

另一个用于序列化,以防万一您想在对象被注销之前做一些事情。

private void writeObject(ObjectOutputStream out) throws IOException {
    // do stuff.
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM