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())
,用设置拼写的坐标,如你所说,创建一个实例。谢谢。