Java 对象相互覆盖

Java 对象相互覆盖,java,object,extends,Java,Object,Extends,我有一个类GameObject: public class GameObject{ private Coordinate coordinates; public GameObject(){ coordinates = new Coordinate(); } public void setCoordinates(int x, int y){ coordinates.x = x; coordinates.y = y

我有一个类
GameObject

public class GameObject{

    private Coordinate coordinates;

    public GameObject(){
        coordinates = new Coordinate();
    }

    public void setCoordinates(int x, int y){
        coordinates.x = x;
        coordinates.y = y;
    }

    //More methods here   
}

public class Coordinate{

    public int x, y;

    public Coordinate(){

    }

    public Coordinate(int x, int y){
        this.x = x;
        this.y = y;
    }

    public void setCoordinate(int x, int y){
        this.x = x;
        this.y = y;
    }
和两个职业
冠军
拼写

public class Spell extends GameObject{
    //Some methods
}

public class Champion extends GameObject{
    //Some methods
    public Spell fireBall = new Spell();
}
在我的
main
课程中:

Champion character=newchampion()

如果我调用character.setCoordinates(200300)(只是随机数),字符指向这些精确的坐标。但是
拼写火球
也会转到
(200300)
。因此
拼写中的
坐标
设置坐标(int x,int y)
调用
字符
覆盖。这怎么可能

TL;DR-来自
游戏对象
的两个类,
拼写扩展游戏对象
冠军扩展游戏对象
,相互覆盖
坐标
。为什么?

完整的源代码:



在类
拼写中设置坐标:

this.startCoordinates = startCoordinates;
setCoordinates(this.startCoordinates);
随后,该代码

if (getCoordinates().x - startCoordinates.x < range) {
if(getCoordinates().x-startCoordinates.x
相当于

if (getCoordinates().x - getCoordinates().x < range) {
if(getCoordinates().x-getCoordinates().x
因为
getCoordinates()
引用的对象与
startCoordinates
引用的对象相同。
setter方法只设置引用,但不复制对象。

在类
拼写中设置坐标:

this.startCoordinates = startCoordinates;
setCoordinates(this.startCoordinates);
随后,该代码

if (getCoordinates().x - startCoordinates.x < range) {
if(getCoordinates().x-startCoordinates.x
相当于

if (getCoordinates().x - getCoordinates().x < range) {
if(getCoordinates().x-getCoordinates().x
因为
getCoordinates()
引用的对象与
startCoordinates
引用的对象相同。
setter方法只设置引用,但不复制对象。

查看gitHub中的代码,您有两种方法:

//Set the coordinates for this GameObject
public void setCoordinates(int x, int y){
    this.coordinates.x = x;
    this.coordinates.y = y;
}

public void setCoordinates(Coordinate coordinates){
    this.coordinates = coordinates;
}
如果使用第二个,则共享同一个坐标实例,因此更改一个将更改另一个

解决方案是复制这些值

public void setCoordinates(Coordinate coordinates){
    this.coordinates.x = coordinates.x;
    this.coordinates.y = coordinates.y;
}

查看gitHub中的代码,您有两种方法:

//Set the coordinates for this GameObject
public void setCoordinates(int x, int y){
    this.coordinates.x = x;
    this.coordinates.y = y;
}

public void setCoordinates(Coordinate coordinates){
    this.coordinates = coordinates;
}
如果使用第二个,则共享同一个坐标实例,因此更改一个将更改另一个

解决方案是复制这些值

public void setCoordinates(Coordinate coordinates){
    this.coordinates.x = coordinates.x;
    this.coordinates.y = coordinates.y;
}

因为只有一个坐标实例?您在哪里创建坐标实例?坐标是静态变量?它们相互覆盖是什么意思?一个
拼写
对象和一个
冠军
对象都应该有自己的
坐标实例,并且不会共享它们。Y你还没有为
游戏对象
显示构造函数,但是每个对象的
坐标
字段都应该在那里初始化。这不是原因。而是因为
坐标
字段是如何初始化的。因为只有一个坐标实例?你在哪里创建
坐标实例
?坐标是静态变量吗?你说它们互相覆盖是什么意思?
拼写
对象和
冠军
对象都应该有自己的
坐标实例,并且不会共享它们。你没有显示
游戏对象
的构造函数,但是
坐标
字段每个对象都要在那里初始化。这不是原因。相反,它必须是在
坐标
字段的初始化方式中。是的,这解决了它。我调用了
拼写.fire(character.getCoordinates())
,用
设置坐标(坐标坐标)设置
拼写
坐标
和创建一个实例,如你所说。谢谢。是的,这解决了问题。我调用了
拼写.fire(character.getCoordinates())
,用
设置拼写的
坐标,如你所说,创建一个实例。谢谢。