0

我正在编写 Robocode,现在我有一个编码问题:

我定义了一个类Enemy来存储敌人信息(如航向、方位、速度等),在这个类中我还定义了一个名为方向的私有属性。然后我使用公共 getter 和 setter 来允许我的机器人调用这些属性。但是当我使用时enemy.setDirection(e.getDirection());,NetBean IDE 显示它不正确。有人可以帮助解决这个问题吗?

public class Enemy {
    // ...
    private double direction;

    public double getDirection(ScannedRobotEvent e, AdvancedRobot me) {
        direction = e.getBearing() + me.getHeading();
        return direction;
    }

    public void setDirection(double direction) {
        this.direction = direction;
    }
}

然后在我的机器人课上:

public class myBot extends AdvancedRobot {
    private Enemy enemy = new Enemy();

    public onScannedRobot(ScannedRobotEvent e) {
        enemy.setDirection(e.getDirection()); // Here is the problem
    }
}
4

2 回答 2

1

当您调用e.getDirection()它时,它会在ScannedRobotEvent. 但getDirection()只为 Enemy 类声明。

于 2015-04-14T11:40:02.407 回答
0

当您调用 getDirection 方法时,您必须输入属性,因此在本例中是 ScannedRobotEvent 和 AdvancedRobot 字段。

于 2014-07-12T05:02:02.677 回答