首页 > 解决方案 > Java序列化与继承?

问题描述

这里是Goals.java

public abstract class Goals {
private String score;

public Goals(String str) {
    this.score = str;
}

String getGoals() {
    return this.score;
}

   void doSomething(score) {
    }
    }

这里是Game.java

public class Game implements Serializable {
    public String name;
    public int game_num;
    public int opp;
    public int player;
    public Goals goal;

    public Game(int i, int i2, int i3) {
        this.player = i;
        this.game_num = i2;
        this.opp = i3;
    }

    public Game(String str, Goals goal) {
        this.name = str;
        this.goal = goal;
    }

}

我们可以创建一个序列化对象,在它被反序列化并转换为之后Game,它会设置score在里面Goals.java吗?doSomething此外,如果序列化数据来自不受信任的来源,您可以操作/覆盖方法吗?

标签: javaserializationdeserialization

解决方案


在你试图操纵你的对象的方式中,我认为你可以这样做,目标实现Serializable,游戏实现目标:

public abstract class Goals implements Serializable{
private String score;

public Goals(String str) {
    this.score = str;
}

String getGoals() {
    return this.score;
}

   void doSomething(score) {
    }
    }

游戏

public class Game extends Goals {
    public String name;
    public int game_num;
    public int opp;
    public int player;

    public Game(int i, int i2, int i3) {
        this.player = i;
        this.game_num = i2;
        this.opp = i3;
    }

    public Game(String str) {
        //create constructor also including the properties of Goals
        Super()...
    }

}

推荐阅读