我是 java 新手,在调用非静态方法时遇到问题
这是我的主要
public static void main(String[] args) {
Fish f1 = new Fish("Nemo");
Fish f2 = new Fish("Dory");
f2.setNumber(2);
Fish m = new Fish("Bruce");
m.setNumber(3);
Fish.printAllFish();
}
这是我的鱼课
import java.util.ArrayList;
import java.util.List;
public class Fish {
protected String name;
protected int number;
protected List<Fish> fishList = new ArrayList<Fish>();
public Fish(String in){
name = in;
number = 1;
fishList.add(this);
}
public void printFish(){
System.out.println("the fish called" + name + " is number " + number );
}
public void setNumber(int Number){
this.number = number;
}
public int getNumber(){
return number;
}
public String getName(){
return name;
}
public int getFishNumOf(){
return fishList.size();
}
public void printAllFish(){
int size = this.getFishNumOf();
System.out.println("There are " + size + " fish:");
for (int i = 0; i < size; i++){
String a = getName();
int b = getNumber();
System.out.println("The Fish " + a + " is number " + b );
}
}
}
尝试调用 printAllFish 时出现非静态错误,我知道我可能会犯一些新手错误,但我才刚刚开始学习编程,类、gets 和 set 之类的东西仍然让我感到困惑,非常感谢任何帮助!