当我意识到我无法创建一个新对象(并在该方法中使用它)。关键是我的有效表示需要被破译,因此我不能直接分配我的字段。代码如下:
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"
}
}
现在我复制了我得到的对象中的所有字段,但它看起来很丑,我想知道是否有更好的方法来实现这样的事情?
最佳答案
问题是您的encode
和decode
方法不一致。 decode
是静态方法,而 encode
则不是。我的建议是使 decode
非静态。
关于java - 有没有办法让 readExternal() 使用另一个构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32180163/