对象变量独立

Object variable independence

正在 java 中重新创建口袋妖怪,但有一个小问题

出于某种原因,当将 Move 对象的相同实例分配给两个不同的 Pokemon 时,当一个 Pokemon 使用该招式时,另一个 Pokemon 招式的 PP 值也会下降一个,而它们应该独立于另一个。我做了很多自学,到目前为止,我认为静态变量是在想要实现类似目标时使用的,但我在这里没有使用任何静态变量,所以我不确定为什么会这样。我是否需要创建相同移动的不同实例来避免这种情况,或者当移动变量属于两个单独的 'Pokemon' 对象时,是否有更简单的方法使它们独立?我在下面包含了我的 Move class 的一些代码。

public class Move
{
    private final String name;
    private final String type;
    private int damage;
    private double accuracy;
    private int powerPoints;
    private int maxPowerPoints;
    private boolean priority; // label for speed priority
    private boolean physical; // physical label
    private boolean special; // special label
}

Do I need to create different instances of the same move to avoid this or is there a simpler way for the Move's variables to be independent when they belong to two separate 'Pokemon' objects?

是的,您应该创建一个新实例。 在 java 中阅读更多关于参考文献的信息,即此处 http://www.informit.com/articles/article.aspx?p=174371&seqNum=4