实现 Externalizable 时是否需要显式默认构造函数?
Do I need explicit default constructor when implementing Externalizable?
我知道如果 class A
实现 Externalizable 它应该有无参数构造函数,但是如果 class 没有任何构造函数(比如我的 A
class) java 为它提供空的无参数构造函数。那为什么我在那里有错误?如果我明确地将无参数构造函数 (public A() {}
) 添加到 A
class 一切都会好的。
错误:
Exception in thread "main" java.io.InvalidClassException: A; no valid constructor
at java.base/java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(ObjectStreamClass.java:159)
at java.base/java.io.ObjectStreamClass.checkDeserialize(ObjectStreamClass.java:864)
at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2061)
at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1594)
at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:430)
at Test.main(Test.java:19)
import java.io.*;
public class Test implements Serializable
{
public static void main(String[] args) throws IOException, ClassNotFoundException
{
// initiaizing
A a1 = new A();
// serializing
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(a1);
oos.close();
// deserializing
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
A a2 = (A) ois.readObject();
}
}
class A implements Externalizable
{
@Override
public void writeExternal(ObjectOutput objectOutput) throws IOException {}
@Override
public void readExternal(ObjectInput objectInput) throws IOException, ClassNotFoundException {}
}
Externalizable 需要 public
无参数构造函数,但前提是无参数构造函数具有默认访问修饰符(也称为包私有)
试试这个:public class A() {}
我知道如果 class A
实现 Externalizable 它应该有无参数构造函数,但是如果 class 没有任何构造函数(比如我的 A
class) java 为它提供空的无参数构造函数。那为什么我在那里有错误?如果我明确地将无参数构造函数 (public A() {}
) 添加到 A
class 一切都会好的。
错误:
Exception in thread "main" java.io.InvalidClassException: A; no valid constructor at java.base/java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(ObjectStreamClass.java:159) at java.base/java.io.ObjectStreamClass.checkDeserialize(ObjectStreamClass.java:864) at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2061) at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1594) at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:430) at Test.main(Test.java:19)
import java.io.*;
public class Test implements Serializable
{
public static void main(String[] args) throws IOException, ClassNotFoundException
{
// initiaizing
A a1 = new A();
// serializing
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(a1);
oos.close();
// deserializing
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
A a2 = (A) ois.readObject();
}
}
class A implements Externalizable
{
@Override
public void writeExternal(ObjectOutput objectOutput) throws IOException {}
@Override
public void readExternal(ObjectInput objectInput) throws IOException, ClassNotFoundException {}
}
Externalizable 需要 public
无参数构造函数,但前提是无参数构造函数具有默认访问修饰符(也称为包私有)
试试这个:public class A() {}