java - 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
此外,如果序列化数据来自不受信任的来源,您可以操作/覆盖方法吗?
解决方案
在你试图操纵你的对象的方式中,我认为你可以这样做,目标实现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()...
}
}
推荐阅读
- aggregate - 像 R 或 pandas 一样在 Julia 中聚合
- xamarin.forms.listview - 滚动时按 10 加载列表视图项
- css - 没有标签的引导自定义单选框未对齐
- python - 比较两个图像 - python,openCV
- css - 将鼠标悬停在容器上时更改 svg 路径的颜色
- macos - OS X 菜单背景颜色
- jenkins - 插件 [id: 'org.sonarqube', version: '2.6.2'] 未找到
- php - 有没有办法在 PHP 中获取 HTTP 状态代码的文本表示?
- c++ - 具有默认参数的函数对象的 SFINAE
- docker - WLST 数据源和未知主机