本帖最后由 宋超2356 于 2014-3-27 16:47 编辑
- class SuperClass {
- private int n;
-
-
- SuperClass() {
- System.out.println("SuperClass()");
- }
-
-
-
- /*SuperClass(int n) {
- System.out.println("SuperClass(" + n + ")"); 这里都注释掉了
- this.n = n;
- }*/
- }
- class SubClass extends SuperClass {
- private int n;
-
- SubClass(int n) {
- //super(n);
- System.out.println("SubClass(" + n + ")");
- this.n = n;
- }
-
- SubClass() {
- //super();
- System.out.println("SubClass()");
- }
- }
- public class Test {
- public static void main(String arg[]) {
- SubClass sc1 = new SubClass(400);
- SubClass sc2 = new SubClass();
- }
- }
复制代码
所有继承不是都要有调用父类的构造函数么,就是都要写上super()在子类构造函数的第一行,这样的没调用也都可以执行?是什么机制? |