我正在尝试创建一个函数,该函数使用BasicAsteroid类型的(x,y,速度x,速度y)值制作一个随机小行星,这是创建随机小行星的构造函数和函数:

private double x, y;
private double vx, vy;

public BasicAsteroid(double x, double y, double vx, double vy) {
    this.x = x;
    this.x = y;
    this.vx = vx;
    this.vy = vy;
}

public static BasicAsteroid makeRandomAsteroid() {
    Random rand = new Random();
    BasicAsteroid x = new BasicAsteroid((rand.nextInt()%FRAME_WIDTH), (rand.nextInt()%FRAME_HEIGHT), (rand.nextInt()%MAX_SPEED), (rand.nextInt()%MAX_SPEED));
    System.out.println(x);
    return x;
}


但是,这是我创建小行星时的输出:

game1.BasicAsteroid@6773120a

game1.BasicAsteroid@4261b6b3

game1.BasicAsteroid@2673b915

game1.BasicAsteroid@113eb90b

game1.BasicAsteroid@1abcc522

如何输出值而不是class @ hashcode?

谢谢。

最佳答案

覆盖toString()方法

@Override
public String toString(){
     return "Asteroid at "+x+" "+y+" velocity "+vx+" "+vy;
}

关于java - 从HashCode Java获取值(value),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36012216/

10-09 03:25