题目如下:在学校的人事管理系统中,教师与学生共同拥有的信息为,编号、姓名、性别、出生日期等;教师特有的信息为所在部门、职称、工资等;学生特有信息为高考分数、所学专业,将教师与学生共同拥有的部分,定义为人员类PersonClass(作为父类),并在此基础上定义教师类TercharClass和学生类StudentClass两个子类。在抽象父类PersonClass中添加抽象方法,在子类教师类TercharClass和学生类StudentClass中实现该方法。编写含main函数的主类,测试抽象类。请主意是测试抽象类,请写出代码
时间: 2024-03-01 16:52:38 浏览: 96
以下是Java代码示例:
```
abstract class PersonClass {
private int id;
private String name;
private String gender;
private String birthDate;
public PersonClass(int id, String name, String gender, String birthDate) {
this.id = id;
this.name = name;
this.gender = gender;
this.birthDate = birthDate;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public String getBirthDate() {
return birthDate;
}
public abstract void printInfo();
}
class TercharClass extends PersonClass {
private String department;
private String jobTitle;
private double salary;
public TercharClass(int id, String name, String gender, String birthDate, String department, String jobTitle, double salary) {
super(id, name, gender, birthDate);
this.department = department;
this.jobTitle = jobTitle;
this.salary = salary;
}
public String getDepartment() {
return department;
}
public String getJobTitle() {
return jobTitle;
}
public double getSalary() {
return salary;
}
@Override
public void printInfo() {
System.out.println("教师信息:");
System.out.println("编号:" + getId());
System.out.println("姓名:" + getName());
System.out.println("性别:" + getGender());
System.out.println("出生日期:" + getBirthDate());
System.out.println("所在部门:" + department);
System.out.println("职称:" + jobTitle);
System.out.println("工资:" + salary);
}
}
class StudentClass extends PersonClass {
private int entranceScore;
private String major;
public StudentClass(int id, String name, String gender, String birthDate, int entranceScore, String major) {
super(id, name, gender, birthDate);
this.entranceScore = entranceScore;
this.major = major;
}
public int getEntranceScore() {
return entranceScore;
}
public String getMajor() {
return major;
}
@Override
public void printInfo() {
System.out.println("学生信息:");
System.out.println("编号:" + getId());
System.out.println("姓名:" + getName());
System.out.println("性别:" + getGender());
System.out.println("出生日期:" + getBirthDate());
System.out.println("高考分数:" + entranceScore);
System.out.println("所学专业:" + major);
}
}
public class MainClass {
public static void main(String[] args) {
PersonClass person1 = new TercharClass(1001, "张三", "男", "1998-01-01", "计算机科学与技术", "副教授", 8000.0);
PersonClass person2 = new StudentClass(2001, "李四", "女", "2000-01-01", 700, "计算机科学与技术");
person1.printInfo();
person2.printInfo();
}
}
```
在上述代码中,PersonClass类为抽象类,其中包含了编号、姓名、性别、出生日期等共同拥有的信息,并定义了一个抽象方法printInfo。TercharClass和StudentClass分别为教师类和学生类,继承自PersonClass类,并实现了printInfo方法,按照各自的信息输出。在MainClass中,创建了一个教师对象和一个学生对象,并分别调用了printInfo方法,输出各自的信息。
阅读全文