子类继承父类,首先应该完成父类的构造函数,因为有可能后面会用到或者修改父类的一些资源,因此得先初始化父类
class Person{
private String name;
public Person(String name){
this.name=name;
}
}
public class Teacher extends Person{
private int age;
public Teacher(String name,int age){
super(); //等价与 this.name=name;
this.age=age;
}
public void speak(){
System.out.println(" 我要进云八!");
}
public static void main(String[] args){
Teacher t=new Teacher("zhangSann",23);
t.speak();
}
}
|