t****r 发帖数: 25 | 1 Parent.java:
public class Parent {
public Parent() {
System.out.println("parent");
}
}
Child.java:
public class Child extends Parent {
public Child() {
System.out.println("child");
}
public static void main(String[] args) {
Child c = new Child();
}
}
java Child, outputs:
parent
child
WHY?? |
g*s 发帖数: 2277 | 2 If you don't call super(xxx) or this(xxx) as the first line of code in a
constructor, Java will automatically insert a call to the default no-
argument superclass constructor super() for you.
【在 t****r 的大作中提到】 : Parent.java: : public class Parent { : public Parent() { : System.out.println("parent"); : } : } : Child.java: : public class Child extends Parent { : public Child() { : System.out.println("child");
|
g*s 发帖数: 2277 | 3 If you don't call super(xxx) or this(xxx) as the first line of code in a
constructor, Java will automatically insert a call to the default no-
argument superclass constructor super() for you.
【在 t****r 的大作中提到】 : Parent.java: : public class Parent { : public Parent() { : System.out.println("parent"); : } : } : Child.java: : public class Child extends Parent { : public Child() { : System.out.println("child");
|
B******N 发帖数: 445 | 4 you can use a debugger to step through, which will help you to understand
the instantiation sequence of every bit, and that will help you to
understand more OO.
【在 t****r 的大作中提到】 : Parent.java: : public class Parent { : public Parent() { : System.out.println("parent"); : } : } : Child.java: : public class Child extends Parent { : public Child() { : System.out.println("child");
|