1

当我意识到我无法在该方法中创建新对象(并使用它)时,我试图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"
    }
}

现在我从我得到的对象中复制所有字段,但它看起来很难看,我想知道是否有更好的方法来处理这样的事情?

4

1 回答 1

-2

问题是你encodedecode方法不一致。这decode是一个静态方法,encode而不是。我的建议是使decode非静态的。

于 2015-08-24T11:43:39.713 回答