有人可以检查我的代码并确定为什么我的取货功能不起作用。
-- 为这些程序的发生编写缺失的方法
- 狗鲍勃=新狗(5);
- System.out.println(bob);
- bob.walk();
- bob.walk();
- bob.pickUp("网球");
- System.out.println(bob);
- bob.drop();
- bob.bark();
- System.out.println(bob);
-- 在我定义了 pickUp 方法的课堂上:
public Head()
{
}
public void pickUp(String object)
{
this.object = object;
System.out.println("Picked up "+object);
}
public String getObject()
{
return object;
}
public void drop()
{
System.out.println("Dropped "+object);
object = null;
}
public void bark()
{
System.out.println("WOOF!");
}
public String toString()
{
return "Head is holding "+ object;
}
}
- 我正在使用该方法的另一个类:
public class Dog
{
private int position;
private Leg hind1;
private Leg hind2;
private Leg front1;
private Leg front2;
private Head head = new Head();
//Constructor for Dog class
public Dog(int position)
{
hind1 = new Leg(position-2);
hind2 = new Leg(position-2);
front1 = new Leg(position+2);
front2 = new Leg(position+2);
}
public void walk()
{
front1.step();
front2.step();
hind1.step();
hind2.step();
System.out.println("Pitter patter...");
}
public String toString()
{
return "Head is holding " + head+", Leg at "+hind1.position+", Leg at "+ hind2.position+", Leg at "+front1.position+", Leg at "+front2.position;
}
public void pickup()
{
head.pickUp(head.object);
}
public void drop()
{
head.drop();
}
public void bark()
{
head.bark();
}
PS。head 是我制作的一个新对象,它属于 Head 类,这是第一个代码的来源。我目前正在尝试让第二个代码工作以显示头部拾取的拾取对象。
-编辑:即使我确实输入了一个字符串(例如head.pickUp(“ball”);),当我尝试运行程序时它仍然显示“找不到符号-method.pickUp(java.lang.String)” .