0

使用坐标时我在格式化时遇到问题。

public class Coordinate {
  public int x;
  public int y;

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

所以,后来,当我试图找到我的兔子的位置时,我使用:

    Coordinate (x, y) = rabbit.get(i);

这不起作用,但这确实:

    Coordinate z = rabbit.get(i);

我想找到 x 和 y 值,所以我对如何做到这一点以及为什么 Coordinate (x, y) 不起作用感到困惑。谢谢你的帮助!

4

1 回答 1

2

由于您的属性 x,yCoordinatepublic

Coordinate z = rabbit.get(i);
int xCor = z.x; //this is your x coordinate
int yCor = z.y; //this is your y coordinate

通常这些属性是private,您可以使用 getter/setter-Method 访问它们:

public class Coordinate {
  private int x;
  private int y;

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

  public int getX(){
    return this.x;
  }

  public void setX(int newX){
    this.x = newX;
  }
  //same for Y
}

//in the main program.
    Coordinate z = rabbit.get(i);
    int yourX = z.getX() //this is your x coordinate
    int yourY = z.getY() //this is your y coordinate

我假设您使用 Java,所以我添加了Tag, 这可以突出显示。这以相同的方式适用于其他语言。

于 2012-12-02T20:55:16.993 回答