怎么不能运行?
class Member {private static int counter = 0;
private int id = ++counter;
public Member() {
System.out.println(
"Member constructor " + id);
}
}
class Rodent3 {
Member m = new Member();
public Rodent3() {
System.out.println("Rodent constructor");
}
public void hop() {
System.out.println("Rodent hopping");
}
public void scurry() {
System.out.println("Rodent scurrying");
}
public void reproduce() {
System.out.println("Making more Rodents");
}
public String toString() {
return "Rodent";
}
}
class Mouse3 extends Rodent3 {
Member m = new Member();
public Mouse3() {
System.out.println("Mouse constructor");
}
public void hop() {
System.out.println("Mouse hopping");
}
public void scurry() {
System.out.println("Mouse scurrying");
}
public void reproduce() {
System.out.println("Making more Mice");
}
public String toString() {
return "Mouse";
}
}
class Gerbil3 extends Rodent3 {
Member m = new Member();
public Gerbil3() {
System.out.println("Gerbil constructor");
}
public void hop() {
System.out.println("Gerbil hopping");
}
public void scurry() {
System.out.println("Gerbil scurrying");
}
public void reproduce() {
System.out.println("Making more Gerbils");
}
public String toString() {
return "Gerbil";
}
}
class Hamster3 extends Rodent3 {
Member m = new Member();
public Hamster3() {
System.out.println("Hamster constructor");
}
public void hop() {
System.out.println("Hamster hopping");
}
public void scurry() {
System.out.println("Hamster scurrying");
}
public void reproduce() {
System.out.println("Making more Hamsters");
}
public String toString() {
return "Hamster";
}
}
public class E10_RodentInitialization {
public static void main(String args[]) {
new Hamster3();
}
}
输出应该是:
Member constructor 1
Member constructor 2
Rodent constructor
Member constructor 7
Member constructor 8
Hamster constructor