构造函数


class A{

public A(){
System.out.println("调用A的无参构造函数");
}

public A(String temp){
System.out.println("调用A的有参构造函数"+temp);
}
}
/*
使用super调用父类构造器的语句必须是子类构造器的第一条语句

如果子类构造器没有显式地调用父类的构造器,则将自动调用父类的默认(没有参数)的构造器。
如果父类没有不带参数的构造器,并且在子类的构造器中又没有显式地调用父类的构造器,则java编译器将报告错误
*/
class B extends A{

public B(){
System.out.println("调用B的无参构造函数");
}

public B(String temp){
// super(temp);
System.out.println("调用B的有参构造函数"+temp);
}

}

public class Test1 {

public static void main(String[] args) {
B b_1 = new B();
B b_2 = new B("你好");
}

}

posted @ 2017-05-16 00:18  Jachin  阅读(131)  评论(0编辑  收藏  举报