不确定为什么,但我在第一个花括号上遇到语法错误,它说我需要在声明第一个变量后添加一个右括号。
public class DogNeeds extends PetNeeds {
super.setAnimalType("dog"); //This method is inherited from PetNeeds superclass
protected boolean walk;
public DogNeeds(String name) {
super(name);
// TODO Auto-generated constructor stub
}
boolean getWalk() {
return walk;
}
void setWalk(boolean walk) {
this.walk = walk;
}
public void walkDog() {
if(walk) {
System.out.println("Time to walk " + super.getName());
}
else {
System.out.println(super.getName() + " doesn't need to go out yet.");
}
}
}
答案 0 :(得分:1)
这一行:
super.setAnimalType("dog"); //This method is inherited from PetNeeds superclass
必须位于方法或构造函数中。
答案 1 :(得分:1)
super.setAnimalType("dog")
不能在方法之外。您可以将其添加到任何方法或重写方法。例如:
public class DogNeeds extends PetNeeds {
protected boolean walk;
public DogNeeds(String name) {
super(name);
// TODO Auto-generated constructor stub
}
void setAnimalType(String animal) {
super.setAnimalType(animal);
}
boolean getWalk() {
return walk;
}
void setWalk(boolean walk) {
this.walk = walk;
}
public void walkDog() {
if(walk) {
System.out.println("Time to walk " + super.getName());
}
else {
System.out.println(super.getName() + " doesn't need to go out
yet.");
}
}
}
答案 2 :(得分:1)
super.setAnimalType("dog");
当你调用这样的方法时,它应该进入你自己的方法。由于它不是任何类型的声明,因此应该在函数/方法中的某处执行。
P.S。我今天早上犯了一个类似的错误,愚蠢的我。