abstract class Person { protected String name; protected int age; public Person(String name, int age) { this.name = name; this.age = age; } public abstract void display(); } class Student extends Person { int studentId; String major; public Student(String name, int age, int studentId, String major) { super(name, age); this.studentId = studentId; this.major = major; } @Override public void display() { System.out.println("学生信息 - 姓名:" + name + ", 年龄:" + age + ", 学号:" + studentId + ", 专业:" + major); } } class Teacher extends Person { String subject; double salary; public Teacher(String name, int age, String subject, double salary) { super(name, age); this.subject = subject; this.salary = salary; } @Override public void display() { System.out.println("教师信息 - 姓名:" + name + ", 年龄:" + age + ", 科目:" + subject + ", 薪资:" + salary); } } public class PolymorphismTest { public static void addPerson(Person[] array, int count, Person p) { if (count < array.length) { array[count] = p; } } public static void main(String[] args) { Person[] persons = new Person[10]; int count = 0; System.out.println("=== 挑战题测试 ==="); persons[count++] = new Student("张三", 20, 1001, "计算机科学"); persons[count++] = new Student("李四", 22, 1002, "软件工程"); persons[count++] = new Teacher("王老师", 35, "数据结构", 8000); persons[count++] = new Teacher("刘老师", 40, "算法分析", 9000); System.out.println("添加成功!共添加 " + count + " 人\n"); System.out.println("=== 所有人员信息 ==="); for (int i = 0; i < count; i++) { persons[i].display(); } System.out.println("\n=== 思考题解答 ==="); System.out.println("多态优势:addPerson(Person p) 统一处理不同类型"); System.out.println("添加特定属性问题:使用 instanceof 判断类型后强制转换"); } }