父类构造方法

class People {
    String name;

//    public People() {
//        System.out.print(1);
//    }

    public People(String name) {
        System.out.print(2);
        this.name = name;
    }
}

class Child extends People {
    People father;

    public Child(String name) {
//        super(name);
        System.out.print(3);
        this.name = name;
        father = new People(name + ":F");
    }

    public Child() {
//        super("");
        System.out.print(4);
    }

}

 

 

如果父类没有无参的构造方法,子类的两个构造方法都会报错,会有提示错误,除非子类明确用了父类的有参构造方法

 

如果 父类没有带参数的构造方法,则会自动调用系统默认为父类添加的无参的构造方法

posted @ 2017-05-08 16:46  antball  阅读(352)  评论(0编辑  收藏  举报