题目如下:在学校的人事管理系统中,教师与学生共同拥有的信息为,编号、姓名、性别、出生日期等;教师特有的信息为所在部门、职称、工资等;学生特有信息为高考分数、所学专业,将教师与学生共同拥有的部分,定义为人员类PersonClass(作为父类),并在此基础上定义教师类TercharClass和学生类StudentClass两个子类。在PersonClass中添加方法,在子类教师类TercharClass和学生类StudentClass中重写该方法。编写含main函数的主类,测试多态。请主意是测试多态,请写出代码
时间: 2024-03-01 13:52:20 浏览: 195
PersonClass类的代码如下:
```java
public class PersonClass {
private String id;
private String name;
private String gender;
private String birth;
public PersonClass(String id, String name, String gender, String birth) {
this.id = id;
this.name = name;
this.gender = gender;
this.birth = birth;
}
public void display() {
System.out.println("编号:" + id);
System.out.println("姓名:" + name);
System.out.println("性别:" + gender);
System.out.println("出生日期:" + birth);
}
}
```
TercharClass类的代码如下:
```java
public class TercharClass extends PersonClass {
private String department;
private String title;
private double salary;
public TercharClass(String id, String name, String gender, String birth, String department, String title, double salary) {
super(id, name, gender, birth);
this.department = department;
this.title = title;
this.salary = salary;
}
@Override
public void display() {
super.display();
System.out.println("所在部门:" + department);
System.out.println("职称:" + title);
System.out.println("工资:" + salary);
}
}
```
StudentClass类的代码如下:
```java
public class StudentClass extends PersonClass {
private double score;
private String major;
public StudentClass(String id, String name, String gender, String birth, double score, String major) {
super(id, name, gender, birth);
this.score = score;
this.major = major;
}
@Override
public void display() {
super.display();
System.out.println("高考分数:" + score);
System.out.println("所学专业:" + major);
}
}
```
测试多态的主类代码如下:
```java
public class TestClass {
public static void main(String[] args) {
PersonClass p1 = new TercharClass("20001", "张三", "男", "1980-01-01", "计算机系", "副教授", 8000);
PersonClass p2 = new StudentClass("20180101", "李四", "男", "2000-01-01", 600, "计算机科学与技术");
p1.display();
System.out.println("===================================");
p2.display();
}
}
```
输出结果如下:
```
编号:20001
姓名:张三
性别:男
出生日期:1980-01-01
所在部门:计算机系
职称:副教授
工资:8000.0
===================================
编号:20180101
姓名:李四
性别:男
出生日期:2000-01-01
高考分数:600.0
所学专业:计算机科学与技术
```
阅读全文