package study;
class Person{
private String name ;
private int age ;
public Person(String name,int age){
this.name = name ;
this.age = age ;
}
public String getName(){
return this.name ;
}
public int getAge(){
return this.age ;
}
public String getInfo(){
return "姓名:" + this.getName() + ";年龄:" + this.getAge();
}
//在定义一个类时,如果没有特殊需求,尽量在类中定义一个无参的构造方法,避免被继承时出现错误
public Person() {
}
}
class Student extends Person{
private String school ;
public Student(){
}
public Student(String name,int age){
this() ;
}
public Student(String name,int age,String school){
this(name,age) ;
this.school = school ;
}
public String getSchool(){
return this.school ;
}
public String getInfo(){
return super.getInfo() + ";学校:" + this.getSchool() ;
}
}
public class exercise{
public static void main(String arsg[]){
Student stu = new Student("张三",30,"清华大学") ;
System.out.println(stu.getInfo()) ;
}
};
9.1继承时最好定义一个无参的构造方法
最新推荐文章于 2022-12-11 09:48:43 发布