【练习】
题目要求:
*子类Student类继承自Person类,
* 新增成员变量:
* (1)所在学校:school, 私有 String类型
* 构造方法:
* (1)构造方法1:不带参数,各成员变量初始化为 姓名:小明, 年龄:18 性别:男 ,所在学校:某某学校
* (2)构造方法2:带4个参数
* 成员方法:
* (1)get/set方法:提供school属性的get/set方法
* (2)打印学生信息 void showInfo():打印姓名、年龄、性别、学校。(注意super关键字使用)
class Student extends Person{
private String school;
public Student(){
super();
school = "某某学校";
}
public Student(String name,int age,String sex,String school){
super(name,age,sex);
this.school = "某某学校";
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school= school;
}
public void showInfo(){
System.out.println("姓名:" + super.getName());
System.out.println("年龄:" + super.getAge());
System.out.println("性别:" + super.getSex());
System.out.println("学校:" +school);
}
}
public class TestStudent {
public static void main(String[] args) {
Student school = new Student("Snowy",22,"女","某某学校");
school .showInfo();
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f024c3b9d3520777b969a3641645326c.png)