Java: 具有 Runnable 成员和复制构造函数的对象的深拷贝
Java: deep copy of object with Runnable member with copy constructor
我有一个 class 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
的其他成员不同,因为关注的部分存储在函数而不是属性中...
如果你知道的唯一一件事,比如说,combatAI
就是它是 Runnable
那么你就不能深度复制它 - 它可以说没有深度 - 它只是 Runnable
。
您可能需要重新考虑这种方法并在游戏中明确设置检查点 (?),以便您可以根据检查点数据创建 combatAI
和其余状态。
另请注意,您的 inventory
副本只有一层深,原始 character
和新 character
共享相同的 item
。与 companions
相同
我有一个 class 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
的其他成员不同,因为关注的部分存储在函数而不是属性中...
如果你知道的唯一一件事,比如说,combatAI
就是它是 Runnable
那么你就不能深度复制它 - 它可以说没有深度 - 它只是 Runnable
。
您可能需要重新考虑这种方法并在游戏中明确设置检查点 (?),以便您可以根据检查点数据创建 combatAI
和其余状态。
另请注意,您的 inventory
副本只有一层深,原始 character
和新 character
共享相同的 item
。与 companions