- super();子类构造方法中如果什么都没有写,则默认赠送super();
- 显示的通过写super(参数)去调用父类的构造方法,则不再默认赠送
- this(参数);调用本类的其他构造方法,this(参数)不能递归
- 创建子类对象的时候,一定会调用到父类的构造方法。
先创建父类;
public class Fu {
public String name;
public int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Fu() {
super();
}
public Fu(String name, int age) {
super();
this.name = name;
this.age = age;
}
public void lookBook() {
System.out.println(age+"岁"+name+"在看书");
}
}
在创建子类;
public class Zi extends Fu {
private String color;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Zi() {
super();
}
public Zi(String name, int age, String color) {
super(name, age);
this.color = color;
}
最后在创建测试类:
public class Test {
public static void main(String[] args) {
Fu fu = new Fu("王羲之", 22);
fu.lookBook();
}
}
打印结果为:
22岁王羲之在看书