重构后反序列化序列化对象

时间:2014-08-06 22:19:50

标签: java serialization deserialization

假设我有以下课程:

public class Foo implements Serializable {
    private static final long serialVersionUID = 1L;
    private String someName;

    public void setName(String name) {
        this.someName = name;
    }

    public String getName() {
        return this.someName;
    }
}

我序列化了这个类的许多对象,并将它们保存到磁盘上。后来,我意识到将这个结构抽象到一个抽象类是有益的,让我有以下两个类:

public abstract class AbstractFoo implements Serializable {
    private static final long serialVersionUID = 1L;
    private String someName;

    public void setName(String name) {
        this.someName = name;
    }

    public String getName() {
        return this.someName;
    }
}

public class Foo extends AbstractFoo implements Serializable {
    private static final long serialVersionUID = 1L;
}

是否可以从之前反序列化文件?在我的测试中尝试这样做不会导致错误,但会导致属性为null

0 个答案:

没有答案