package Test;
public class A {
public A() {
System.out.println("Enter construct A...");
init();
}
public void init() {
System.out.println("Enter A's init...");
}
}
package Test;
public class B extends A {
int i = 0;
int j;
public void init() {
System.out.println("Enter B's init...");
i = 100;
j = 100;
}
public void printAll(){
System.out.println(i);
System.out.println(j);
}
/**
* @param args
*/
public static void main(String[] args) {
B b = new B();
b.printAll();
}
}
Okay, new B gets first an A get created, first instance fields, then calling B's init() which sets i and j to 100. Then the B gets created, which initializes the i to 0.
Enter construct A...
Enter B's init...
0
100
I suppose:
Enter construct A...
Enter B's init...
100
100
Because the A constructor has to be called bevore B can be constructed and as the class you're instatiating is from type B, the init of B is called (there is no super.init() call so the init method of class B hides the one from the super class)
精彩评论