【发布时间】:2015-08-17 10:12:45
【问题描述】:
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() 正在返回一个子对象。那为什么只调用父类的构造函数,为什么不调用子类
【问题讨论】:
标签: java