标题: java中的继承 [打印本页] 作者: 崔司龙 时间: 2015-6-28 21:21 标题: java中的继承 一、子类中对父类构造函数的调用规则
1、如果不显式指定,子类中的构造函数会默认调用父类中的无参构造
测试代码
class Parent{
private String name;
private int age;
public Parent(){
System.out.println("Person()");
}
public Parent(String name){
System.out.println("Person(String name)");
this.setName(name);
}
public Parent(String name, int age){
System.out.println("Person(String name, int age)");
this.setName(name);
this.setAge(age);
}
public void setName(String name){
this.name = name;
}
public void setAge(int age){
this.age = age;
}
}
class Child extends Parent{
public Child(){
System.out.println("Child()");
}
public Child(String name){
System.out.println("Child(String name)");
}
public Child(String name, int age){
System.out.println("Child(String name, int age)");
}
}
public class ExtendsDemo02{
public static void main(String args[]){
Child c1 = new Child();
Child c2 = new Child("张三");
Child c3 = new Child("张三", 15);
}
}