You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.4 KiB
38 lines
1.4 KiB
/**
|
|
* 学生管理系统:使用多态重构添加人员的方法
|
|
*/
|
|
public class StudentManagementSystem {
|
|
|
|
/**
|
|
* 添加人员(使用多态,统一处理学生和教师)
|
|
* @param p 人员对象(Student或Teacher)
|
|
*/
|
|
public void addPerson(Person p) {
|
|
if (p == null) {
|
|
System.out.println("人员对象为空,无法添加。");
|
|
return;
|
|
}
|
|
|
|
// 通用信息
|
|
System.out.println("添加人员信息:");
|
|
System.out.println("姓名:" + p.getName());
|
|
System.out.println("身份证号:" + p.getId());
|
|
|
|
// 使用instanceof判断具体类型,处理特有属性
|
|
if (p instanceof Student) {
|
|
Student student = (Student) p;
|
|
System.out.println("类型:学生");
|
|
System.out.println("学号:" + student.getStudentId());
|
|
System.out.println("专业:" + student.getMajor());
|
|
} else if (p instanceof Teacher) {
|
|
Teacher teacher = (Teacher) p;
|
|
System.out.println("类型:教师");
|
|
System.out.println("教师编号:" + teacher.getTeacherId());
|
|
System.out.println("教授科目:" + teacher.getSubject());
|
|
} else {
|
|
System.out.println("类型:其他人员");
|
|
}
|
|
|
|
System.out.println("添加成功!\n");
|
|
}
|
|
}
|