public classFather {
Stringname;// 名字
int age;// 年龄
public Father() {
System.out.println("我是父类的无参构造器!!!");
}
public Father(String name) {
System.out.println("我是父类的构造器!!!");
this.name = name;
}
static {
System.out.println("我是爸爸");
}
public void learn() {
System.out.println("名字叫" + name + "的爸爸正在学习!!!");
}
}
子类代码:
public classSon extendsFather {
static{
System.out.println("我是儿子");
}
public Son(String name) {
super(name);
System.out.println("我是子类构造器!!!");
this.name=name;
}
public void learn(){
System.out.println("名字叫+"+name+"的儿子正在学习!!!");
}
}
测试代码:
public classTest {
public static void main(String[] args) {
Sonson=new Son("小明");
son.learn();
}
}