我是java的新手并尝试使用Karel向右转的方法创建一个新的机器人类。但是,当我尝试扩展课程时,#34; RightTurnerRobot"包括" Robot",我收到以下错误:
默认情况下,隐式超级构造函数Robot()未定义 构造函数。必须定义显式构造函数
我的代码如下,任何帮助都表示赞赏。
UrRobot
编辑:我发现了问题。它应该扩展Robot
而不是import kareltherobot.*; //Needed for Robots
import java.awt.*; //Needed for Color
public class RightTurnerRobot extends UrRobot
{
public RightTurnerRobot(int street, int avenue, Direction direction,
int beepers)
{
super(street, avenue, direction, beepers);
// TODO Auto-generated constructor stub
}
public void turnRight()
{
turnLeft();
turnLeft();
turnLeft();
}
}
。然后我用:
{{1}}
答案 0 :(得分:0)
你需要有一个与Robot类匹配的构造函数,因为你正在扩展它。
import kareltherobot.*;
import kareltherobot.Robot;
import java.awt.*;
public class RightTurnerRobot extends Robot implements RobotTask
{
public RightTurnerRobot(int arg0, int arg1, Direction arg2, int arg3) {
super(arg0, arg1, arg2, arg3);
}
public void TurnRight()
{
turnLeft();
turnLeft();
turnLeft();
}
static
{
World.setDelay(100);
World.setSize(10, 10);
}
public void task()
{
//World.readWorld("src", "someworldname.kwld");
UrRobot grace = new UrRobot(3, 3, East, 30);
grace.move();
grace.turnOff();
}
在我看来,更好的方法就是这样。
import kareltherobot.*;
import kareltherobot.Robot;
import java.awt.*;
public class RobotWorld{
//Create Robot
Robot robot;
public RobotWorld () {
//Initialize World
World.readWorld("PATH/TO/WORLD");
World.setVisible(true);
//Initialize Robot
robot = new Robot(3, 3, Robot.East, 3);
turnRight(robot);
}
public void turnRight (Robot tempRobot) {
tempRobot.turnLeft();
tempRobot.turnLeft();
tempRobot.turnLeft();
}
public static void main (String [] args) {
RobotWorld world = new RobotWorld();
}
}