我有一个类character,我想在某个事件之前创建一个副本,这样如果用户搞砸了,他可以重试该副本。为了实现这一点,我使用了这样的复制构造函数:public class character implements Serializable{private String name;private HashMap<String,Integer> values = new HashMap<>();private ArrayList<String> traits = new ArrayList<>();private enums.playerRelations playerRelation;private static character playerCharacter = new character();private transient Runnable combatAI;private transient Runnable tradingAI;private ArrayList<character> companions = new ArrayList<>();private ArrayList<item> inventory = new ArrayList<>();private int maximumInventorySize = 20;private int maximumSkillSlots = 10;private transient ArrayList<Pair<skill,Boolean>> skills = new ArrayList<>();private int portraitResource = R.drawable.ic_bear;private event eventContext;public character(character o){ name = o.name; values = new HashMap<>(o.values); traits = new ArrayList<>(o.traits); playerRelation = o.playerRelation; combatAI = o.combatAI; tradingAI = o.tradingAI; companions = new ArrayList<>(o.companions); inventory = new ArrayList<>(o.inventory); maximumInventorySize = o.maximumInventorySize; maximumSkillSlots = o.maximumSkillSlots; skills = new ArrayList<>(o.skills); portraitResource = o.portraitResource; eventContext = o.eventContext;}// ...}但是,我不知道如何深度克隆Runnable对象。我怎样才能深度复制那些?使用复制构造函数完全可能吗?我注意到Runnable与其他成员不同character的是,关注的部分存储在函数中而不是属性中......
1 回答
米脂
TA贡献1836条经验 获得超3个赞
如果你只知道combatAI
它是,Runnable
那么你不能深度复制它——可以说它没有深度——它只是Runnable
.
您可能想重新考虑这种方法并在游戏中明确设置检查点 (?),以便您可以combatAI
根据检查点数据创建状态和其余状态。
另请注意,您的副本inventory
只有一级深,原始和新character
s 共享相同的item
s。同样处理companions
添加回答
举报
0/150
提交
取消