public class Parent {
public Parent() {
System.out.println("Parent");
}
}
public class Child extends Parent implements Serializable {
public Child() {
System.out.println("Child");
}
}
public class Demote {
public static void main(String[] args) {
Child c=new Child();
try {
FileOutputStream fos=new FileOutputStream
("D:\\DemoFile\\Testword5.txt");
ObjectOutputStream oot=new ObjectOutputStream(fos);
oot.writeObject(c);
FileInputStream fin=new FileInputStream
("D:\\DemoFile\\Testword5.txt");
ObjectInputStream oin=new ObjectInputStream(fin);
oin.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
此代码的输出是 – Parent Child Parent.
调用第一组Parent Child,因为调用了零参数构造函数. oin.readObject()返回一个子对象.那么为什么只调用父类的构造函数,为什么不调用子类