在父类中定义了2个构造函数,在子类中,也定义了2个构造函数。当运行子类时,可以发现先调用父类的构造函数,在调用子类的构造函数。
//实验四:构造函数的继承
//实验内容:在父类中,定义了2个构造函数,在子类中,也定义了2个构造函数。编写
//程序显示父类、子类构造函数的调用过程,在子类中实现构造函数的继承。
package chapter04.test.test4;
//定义父类
class Father{
//不带参数的构造函数
public Father(){
System.out.println("父类中的构造器");
}
//带参数的构造函数
public Father(String s){
System.out.println(s + "父类中的构造器");
}
}
//定义子类
class Child extends Father{
public Child(){
System.out.println("子类中的构造器");
}
public Child(String s){
System.out.println(s +"子类中的构造器");
}
}
public class Test4{
public static void main(String[] args){
Child c = new Child("哈哈");
Child c1 = new Child();
}
}