首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java使用继承进行序列化吗?

Java使用继承进行序列化吗?
EN

Stack Overflow用户
提问于 2018-06-25 10:45:20
回答 2查看 270关注 0票数 1

这是Goals.java

代码语言:javascript
复制
public abstract class Goals {
private String score;

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

String getGoals() {
    return this.score;
}

   void doSomething(score) {
    }
    }

这是Game.java

代码语言:javascript
复制
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后,在Goals.java中设置score?此外,如果序列化数据来自不受信任的来源,您是否可以操作/覆盖doSomething方法?

EN

回答 2

Stack Overflow用户

发布于 2018-06-25 10:56:48

在你试图操纵你的对象的方式中,我认为你可以做到Goals实现Serializable,而游戏实现Goals:

代码语言:javascript
复制
public abstract class Goals implements Serializable{
private String score;

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

String getGoals() {
    return this.score;
}

   void doSomething(score) {
    }
    }

对于游戏

代码语言:javascript
复制
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()...
    }

}
票数 0
EN

Stack Overflow用户

发布于 2018-06-25 11:03:39

正如注释中提到的,使用Serializable实现Goals及其实现,并在这两个类中实现默认构造函数。

工作代码片段:

代码语言:javascript
复制
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");
        }


    }
}

输出:

代码语言:javascript
复制
Object has been serialized
Object has been deserialized 
score = 20

PS:

  1. 建议将比分设置为整数,否则您将无法执行基本操作,如使用内置运算符(+,-)增加比分或减少比分。
  2. 总是放置正确的getter setter名称,如getScore()和setScore int这种情况下。
  3. 确实尝试使构造函数同步,例如,在调用具有玩家id的游戏构造函数时,游戏编号和目标将永远不会被初始化。最好使用默认值或其他更好的方法初始化它们,使另一个构造函数接受所有参数,然后从单个构造函数中调用主构造函数,并为那些未被调用方法传递的构造函数传递默认值。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51015687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档