我有一个类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的是,关注的部分存储在函数中而不是属性中......
米脂
相关分类