首页 新闻 赞助 找找看

父类实现Externalizable接口,在序列化子类的时候,为什么在子类中要自己写无参构造器

0
悬赏园豆:15 [已关闭问题] 关闭于 2019-09-20 14:38

public class TestExternalizable implements Externalizable {
@Override
public void writeExternal(ObjectOutput out) throws IOException { }
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { }

public static void main(String[] args) throws IOException, ClassNotFoundException {
    A a = new A();
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e:/test/b.txt"));
    oos.writeObject(a);
    oos.close();
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e:/test/b.txt"));
    TestExternalizable aa = (TestExternalizable)ois.readObject();
    ois.close();
}

}
class A extends TestExternalizable{
// public A(){
//
// }
}
如果在类A中不写无参构造器,就会报错:Exception in thread "main" java.io.InvalidClassException: com.test.A; no valid constructor

DDiamondd的主页 DDiamondd | 初学一级 | 园豆:186
提问于:2019-09-20 14:37
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册