访问 Java 中的组合对象
Accessing composed objects in Java
新 Java。
我在下面有一个 Player class 的实例 player1。
Player player1 = new Player(0,0);
在 Player 中 class 我编写了一个 Coord 类型的对象坐标(定义如下)。当我在上方实例化 player1 时,"Player is at coordinate 0,0" 按预期显示。
public class Player extends Entity {
public Coord coordinate;
public Player(int x, int y) {
Coord coordinate = new Coord(x,y);
System.out.println(“Player is at coordinate “ + coordinate.getX() + “,”
+ coordinate.getY());
}
}
坐标class定义如下
public class Coord {
private int x;
private int y;
public Coord(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
当我在实例化player1 后尝试访问obj 及其各自的方法时出现问题。当我尝试访问坐标时出现 NullPointerException 错误。
Player player1 = new Player(0,0);
System.out.println(“Player is at coordinate “ + player1.coordinate.getX() +
“,” + player1.coordinate.getY());
我做错了什么?
您没有将 Coord obj;
设为 class 的字段。这可以像
这样简单
public class Player extends Entity {
Coord obj;
public Player(int x, int y) {
obj = new Coord(x,y);
System.out.println(“Player is at coordinate “ + obj.getX() + “,”
+ obj.getY());
}
}
请注意,obj
是一个糟糕的字段名,它在这里具有默认级别的访问权限。一种改进方法可能是
public class Player extends Entity {
private Coord coordinates;
public Player(int x, int y) {
coordinates = new Coord(x,y);
System.out.println(“Player is at coordinate “ + obj.getX() + “,”
+ obj.getY());
}
public Coord getCoordinates() {
return coordinates;
}
}
然后你就可以像
一样使用它了
Player player1 = new Player(0,0);
System.out.println(“Player is at coordinate “
+ player1.getCoordinates().getX()
+ “,” + player1.getCoordinates().getY());
您也可以在 Coord
class 中覆盖 toString()
,然后您可以说
System.out.println(“Player is at coordinate “
+ player1.getCoordinates());
新 Java。
我在下面有一个 Player class 的实例 player1。
Player player1 = new Player(0,0);
在 Player 中 class 我编写了一个 Coord 类型的对象坐标(定义如下)。当我在上方实例化 player1 时,"Player is at coordinate 0,0" 按预期显示。
public class Player extends Entity {
public Coord coordinate;
public Player(int x, int y) {
Coord coordinate = new Coord(x,y);
System.out.println(“Player is at coordinate “ + coordinate.getX() + “,”
+ coordinate.getY());
}
}
坐标class定义如下
public class Coord {
private int x;
private int y;
public Coord(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
当我在实例化player1 后尝试访问obj 及其各自的方法时出现问题。当我尝试访问坐标时出现 NullPointerException 错误。
Player player1 = new Player(0,0);
System.out.println(“Player is at coordinate “ + player1.coordinate.getX() +
“,” + player1.coordinate.getY());
我做错了什么?
您没有将 Coord obj;
设为 class 的字段。这可以像
public class Player extends Entity {
Coord obj;
public Player(int x, int y) {
obj = new Coord(x,y);
System.out.println(“Player is at coordinate “ + obj.getX() + “,”
+ obj.getY());
}
}
请注意,obj
是一个糟糕的字段名,它在这里具有默认级别的访问权限。一种改进方法可能是
public class Player extends Entity {
private Coord coordinates;
public Player(int x, int y) {
coordinates = new Coord(x,y);
System.out.println(“Player is at coordinate “ + obj.getX() + “,”
+ obj.getY());
}
public Coord getCoordinates() {
return coordinates;
}
}
然后你就可以像
一样使用它了Player player1 = new Player(0,0);
System.out.println(“Player is at coordinate “
+ player1.getCoordinates().getX()
+ “,” + player1.getCoordinates().getY());
您也可以在 Coord
class 中覆盖 toString()
,然后您可以说
System.out.println(“Player is at coordinate “
+ player1.getCoordinates());