当我意识到我无法在该方法中创建新对象(并使用它)时,我试图readExternal
从接口实现以更有效地序列化我的大对象。Externalizable
关键是我的有效表示需要被破译,因此我不能直接分配我的字段。代码如下所示:
public class BigObject implements Externalizable {
//lots of fields and methods...
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(this.encode()); //encodes to a BigInteger
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
BigInteger code = (BigInteger) in.readObject();
BigObject bo = BigObject.decode(code);
// now I would like this to be "bo"
}
}
现在我从我得到的对象中复制所有字段,但它看起来很难看,我想知道是否有更好的方法来处理这样的事情?