package cn.edu.bjut.chapter4; public class PersonTester { public static void main(String[] args) { Person p1 = new Student("123456789", "李文慧", '女', "052615", "05"); Person p2 = new Teacher("987654321", "梁化祥", '男', "071235", "教授"); System.out.println(p1.getId() + "\t" + p1.getName() + "\t" + p1.getGender()); //System.out.println(p1.getStudentId() + "\t" + p1.getClassNo()); System.out.println(p2.getId() + "\t" + p2.getName() + "\t" + p2.getGender()); //System.out.println(p2.getTeacherId() + "\t" + p2.getTitle()); if (p1 instanceof Student) { Student stu = (Student) p1; System.out.println(stu.getStudentId() + "\t" + stu.getClassNo()); } if (p2 instanceof Teacher) { Teacher tea = (Teacher) p2; System.out.println(tea.getTeacherId() + "\t" + tea.getTitle()); } } }