Java 超级构造函数未将值传递给render方法

Java 超级构造函数未将值传递给render方法,java,constructor,super,Java,Constructor,Super,我创建了一个类,可以调用它来存储我的类的x和y值。但是,当我调用超级构造函数时,没有传递值 这是我在主类中创建Player类的新实例的地方 p = new Player(getWidth() / 2, getHeight() / 2, this); 这是我的类,我呼吁我的超级构造函数 public class GameObject { public int x; public int y; public GameObject(int x, int y){ this.x = x

我创建了一个类,可以调用它来存储我的类的x和y值。但是,当我调用超级构造函数时,没有传递值

这是我在主类中创建Player类的新实例的地方

p = new Player(getWidth() / 2, getHeight() / 2, this);
这是我的类,我呼吁我的超级构造函数

public class GameObject {

public int x;
public int y;
public GameObject(int x, int y){    
    this.x = x;
    this.y = y;
}
}
public class Player extends GameObject implements EntityA{

private int x = 0;
private int y = 0;
Game game;
BufferedImage spriteSheet;
Rectangle bounds;

public Player(int x, int y, Game game){
    super(x, y);
    this.game = game;
    bounds = new Rectangle(x, y, Game.spriteSize, Game.spriteSize);
    spriteSheet = game.getSpriteSheet();
    System.out.println("x: " + this.x + " this.y: " + y);

}
这是我调用超级构造函数的类

public class GameObject {

public int x;
public int y;
public GameObject(int x, int y){    
    this.x = x;
    this.y = y;
}
}
public class Player extends GameObject implements EntityA{

private int x = 0;
private int y = 0;
Game game;
BufferedImage spriteSheet;
Rectangle bounds;

public Player(int x, int y, Game game){
    super(x, y);
    this.game = game;
    bounds = new Rectangle(x, y, Game.spriteSize, Game.spriteSize);
    spriteSheet = game.getSpriteSheet();
    System.out.println("x: " + this.x + " this.y: " + y);

}

x和y的值都为零。我可以看出我没有正确使用对超级构造函数的调用。有人能告诉我怎么做吗。谢谢。

问题不在于构造函数,而在于这些类的字段。您可以在
GameObject
中定义
public int x
y
,也可以在
Player
中定义
private int x
y


GameObject.x
将被
Player.x
隐藏。但是,
GameObject
的构造函数仍然将
GameObject.x
设置为给定值。如果您随后尝试查询什么是
Player.x
,那么它仍然是零。

Player
构造函数中,这一行

System.out.println("x: " + this.x + " this.y: " + y);
正在使用
Player
版本的
x
y
,这两个版本被代码忽略,因此具有ints的默认初始值,即零。我想你想消除这个

private int x = 0;
private int y = 0;
从您的
Player
类,这将使您的代码访问
GameObject
版本的
x
y
,这是您的目标