如何使用Razor正确遍历HomeController中的静态列表展示学生与人员信息?
实现Student与Personnel列表的遍历展示功能
我来帮你搞定这个遍历展示的问题!核心思路是利用面向对象的继承/多态特性,或者直接通过类型判断来区分不同对象,输出对应的属性信息。下面分步骤给你具体实现方案:
第一步:确认类结构(关键前提)
首先得让Student和Personnel共享一个基类(比如命名为Person),这样你的private static List才能同时存放这两种类型的对象。基类里放两者共有的属性,子类各自扩展专属属性:
// 基类:存放Student和Personnel的共同属性 public class Person { protected String name; protected String surname; protected String email; protected String cellphone; // 构造方法、Getter/Setter 请自行补充 public Person(String name, String surname, String email, String cellphone) { this.name = name; this.surname = surname; this.email = email; this.cellphone = cellphone; } } // Student子类:扩展Age属性 public class Student extends Person { private int age; public Student(String name, String surname, String email, String cellphone, int age) { super(name, surname, email, cellphone); this.age = age; } // Getter/Setter 补充 public int getAge() { return age; } } // Personnel子类:扩展WorkerType和Degree属性 public class Personnel extends Person { private String workerType; private String degree; public Personnel(String name, String surname, String email, String cellphone, String workerType, String degree) { super(name, surname, email, cellphone); this.workerType = workerType; this.degree = degree; } // Getter/Setter 补充 public String getWorkerType() { return workerType; } public String getDegree() { return degree; } }
第二步:在HomeController中定义并初始化列表
把你的static List声明为List<Person>类型,这样就能同时添加Student和Personnel对象:
@Controller public class HomeController { // 定义存放Person子类的静态列表 private static List<Person> personList = new ArrayList<>(); // 初始化示例数据(可以根据实际场景替换) static { personList.add(new Student("John", "Greenberg", "123@123", "123456789", 20)); personList.add(new Personnel("Rose", "Marry", "email@email", "123456789", "Permanent", "BED Education")); personList.add(new Student("Chaz", "Brown", "chazz@gmail.com", "123456789", 30)); } // 接下来实现遍历展示的方法 }
第三步:实现遍历展示功能(两种方案可选)
方案一:用instanceof判断类型(简单直接)
遍历列表时,通过instanceof判断当前对象是Student还是Personnel,然后强转后输出对应属性:
public void displayPersonInfo() { for (Person person : personList) { if (person instanceof Student) { Student student = (Student) person; System.out.printf("[Student] Name:%s Surname:%s Email: %s Cellphone:%s Age: %d ", student.name, student.surname, student.email, student.cellphone, student.getAge()); } else if (person instanceof Personnel) { Personnel personnel = (Personnel) person; System.out.printf("[Personnel] Name:%s Surname:%s Email: %s Cellphone:%s WorkerType: %s Degree: %s ", personnel.name, personnel.surname, personnel.email, personnel.cellphone, personnel.getWorkerType(), personnel.getDegree()); } } }
方案二:用多态实现(更符合面向对象设计)
在基类中定义抽象的展示方法,子类各自实现自己的展示逻辑,遍历的时候直接调用方法即可,不需要判断类型:
- 先修改基类
Person,添加抽象方法:
public abstract class Person { // 原有属性和构造方法不变 public abstract void display(); // 抽象展示方法 }
- 子类实现display方法:
// Student类中实现 @Override public void display() { System.out.printf("[Student] Name:%s Surname:%s Email: %s Cellphone:%s Age: %d ", name, surname, email, cellphone, age); } // Personnel类中实现 @Override public void display() { System.out.printf("[Personnel] Name:%s Surname:%s Email: %s Cellphone:%s WorkerType: %s Degree: %s ", name, surname, email, cellphone, workerType, degree); }
- 遍历方法简化为:
public void displayPersonInfo() { for (Person person : personList) { person.display(); } }
两种方案对比
- 方案一适合简单场景,代码直观,不需要修改原有类结构(如果基类不是抽象类的话);
- 方案二更优雅,扩展性更强——后续如果新增其他Person子类(比如Teacher),只需要实现display方法,不需要修改遍历的代码,完全符合开闭原则。
运行上面的代码,就能得到你想要的输出效果啦!
内容的提问来源于stack exchange,提问作者user8998071
相关产品推荐
相关产品推荐

