class Jerry {
public Jerry() {
System.out.println("Jerry");
}
public void hello() {
System.out.println("Hello in Jerry");
}
}
class Tom extends Jerry {
public Tom() {
System.out.println("Tom");
}
public Tom(String s) {
this();
System.out.println("I am " + s);
}
public void hello() {
System.out.println("Hello in Tom");
}
}
public class Test3 extends Tom {
public Test3() {
this("test");
System.out.println("Test");
}
public Test3(String s) {
super(s);
System.out.println("Hello " + s);
}
public static void main(String args[]) {
Test3 t = new Test3();
t.hello();
}
}
答案
Jerry
Tom
I am test
Hello test
Test
Hello in Tom
this()就是調用自身的無參構造函數。打印的tom就是這個無參構造函數的內容的。