Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 有没有办法让readExternal()使用另一个构造函数?_Java_Serialization_Externalizable - Fatal编程技术网

Java 有没有办法让readExternal()使用另一个构造函数?

Java 有没有办法让readExternal()使用另一个构造函数?,java,serialization,externalizable,Java,Serialization,Externalizable,我试图从Externalizable接口实现readExternal,以便更有效地序列化我的大对象,但我意识到我无法在该方法中创建新对象(并使用它)。关键是我的有效表示需要被破译,因此我不能直接分配字段。代码如下所示: public class BigObject implements Externalizable { //lots of fields and methods... @Override public void writeExternal(ObjectO

我试图从
Externalizable
接口实现
readExternal
,以便更有效地序列化我的大对象,但我意识到我无法在该方法中创建新对象(并使用它)。关键是我的有效表示需要被破译,因此我不能直接分配字段。代码如下所示:

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"
    }
}

现在我复制了我得到的对象的所有字段,但它看起来很难看,我想知道是否有更好的方法来实现这样的效果?

问题是,您的
编码方法和
解码方法不一致。
decode
是一种静态方法,而
encode
不是。我的建议是使
解码
非静态

我想你没有回答我的问题。我有一个可行的解决方案,但我只想让它变得更好。除此之外,我不知道如何使
decode(biginger code)
非静态…
decode
方法是否属于您编写的代码?你能展示一下它的结构吗?