我正在做一个相当简单的程序来了解静态和非静态方法和变量之间的区别,我以为我理解了,但我无法运行以下代码:
public class Zombie {
String name;
int serial = 0;
static int count = 0;
public Zombie(String name) {
this.name = name;
count++;
}
static String printStatus() {
String status;
if(count == 1) {
status = (count + "zombie created so far");
}
else {
status = (count + "zombies created so far");
}
return status;
}
String printZombie() {
String ident = (name + " is zombie " + serial);
return ident;
}
public static void main(String[] args) {
printStatus();
Zombie z1 = new Zombie("Deb");
printStatus();
Zombie z2 = new Zombie("Andy");
printStatus();
Zombie z3 = new Zombie("Carl");
printStatus();
z1.printZombie();
z2.printZombie();
z3.printZombie();
}
}
它应该有一个输出:
0 zombies created so far
1 zombie created so far
2 zombies created so far
3 zombies created so far
Deb is zombie 0
Andy is zombie 1
Carl is zombie 2
但我无法让它运行。我认为问题(至少其中一个)出在第一种方法上,但我无法弄清楚。count 应该是静态的,而其他两个变量不是,并且 printStatus 应该是静态的,但 printZombie 不是。有人可以向我解释一下吗?