- 当创建一个子类对象时,会先调用父类的构造方法,再调用子类的构造方法
- 每一个子类的构造方法默认都会调用父类的无参构造方法
- 当父类没有无参构造时,子类必须手动调用父类构造方法
- super语句和this语句一样,必须写在第一行
- 子类的每一个构造方法都必须直接或间接调用父类构造方法
class Person{
String name;
int age;
Person(String name){
this.name=name;
}
Person(String name,int age){
this(name);
this.age=age;
}
}
class Student extends Person{
Student(String name){
super(name);
}
Student(String name,int age){
this(name);
this.age=age;
}
}
class Fu{
Fu(){
System.out.println("父类无参构造");
}
void show(){
System.out.println("父类show方法");
}
}
class Zi extends Fu{
String name;
Zi(){
System.out.println("子类无参构造");
}
Zi(String name){
System.out.println("子类含参name构造");
this.name=name;
}
}
public class Test{
public static void main(String[] args){
Zi zi= new Zi();
zi.show();
Zi zi1= new Zi("曹操");
Student s1=new Student("刘备");
System.out.println(s1.name);
Student s2=new Student("关羽",20);
System.out.println(s2.name+s2.age);
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d7d01f8f4be65f8d1c85a0a625dd24d9.png)