Java序列化与继承?

时间:2018-06-25 02:45:20

标签: java serialization deserialization

这里是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方法吗?

2 个答案:

答案 0 :(得分:0)

在您尝试操作对象的方式中,我认为您可以做到目标实现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()...
    }

}

答案 1 :(得分:0)

如评论中所述,使用Serializable实现目标及其实现,并在两个类中均实现默认构造函数。

有效的代码段:

null

输出:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

abstract class Goals implements Serializable{
private String score;


public Goals() {
    this(null);
}

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

String getGoals() {
    return this.score;
}

   void doSomething(int score) {
    }
    }

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

}

class GoalImpl extends Goals implements Serializable{
    public GoalImpl() {
        }
    public GoalImpl(String str) {
        super(str);
    }

}

public class Main{
    public static void main(String...s) {
        Goals goal = new GoalImpl("20");
        Game game = new Game("name",goal);

        try
        {   
            //Saving of object in a file
            FileOutputStream file = new FileOutputStream("gamefile.ser");
            ObjectOutputStream out = new ObjectOutputStream(file);


            // Method for serialization of object
            out.writeObject(game);

            out.close();
            file.close();

            System.out.println("Object has been serialized");

        }

        catch(IOException ex)
        {
            ex.printStackTrace();
            System.out.println("IOException is caught");
        }


        Game object1 = null;

        // Deserialization
        try
        {   
            // Reading the object from a file
            FileInputStream file = new FileInputStream("gamefile.ser");
            ObjectInputStream in = new ObjectInputStream(file);

            // Method for deserialization of object
            object1 = (Game)in.readObject();

            in.close();
            file.close();

            System.out.println("Object has been deserialized ");
            System.out.println("score = " + object1.goal.getGoals());
        }

        catch(IOException ex)
        {
            ex.printStackTrace();
            System.out.println("IOException is caught");
        }

        catch(ClassNotFoundException ex)
        {
            System.out.println("ClassNotFoundException is caught");
        }


    }
}

PS:

  1. 将分数设为整数的建议,否则您将无法使用内置运算符(+,-)进行基本操作,例如增加分数或降低分数

  2. 在这种情况下,始终放置正确的getter设置程序名称,如getScore()和setScore。

  3. 请尝试使构造函数同步,例如,在具有玩家ID,游戏编号和对立的Game构造函数的情况下,将永远不会初始化游戏名称和目标,最好使用默认值或其他值初始化它们一个不错的选择是让另一个构造函数接受所有参数,然后从各个构造中调用主构造函数,并为那些未由调用方法传递的参数传递默认值。