首页 > 解决方案 > 将子对象添加到父对象列表并获取属性

问题描述

请考虑这种情况

abstract class Human{
   String ID;
   String demographic;
   int knoledgeSpreadSpeed;

   
}

class Student extends Human {
   private Human connectedTo;
   private final String connectionAddress;

   public Student(String ID, String demographic, int knoledgeSpreadSpeed, String connectionAddress) {
        super(ID, demographic, knoledgeSpreadSpeed);
        this.connectionAddress = connectionAddress;
    }
}

class Professor extends Human{
    private int numberOfAvailableMentoringSlots;
    private static HashMap<String, Human> socialConnections = new HashMap<>();

    public Professor(String ID, String demographic, int knoledgeSpreadSpeed, int numberOfAvailableMentoringSlots) {
        super(ID, demographic, knoledgeSpreadSpeed);
        this.numberOfAvailableMentoringSlots = numberOfAvailableMentoringSlo;
    }
}

List <Human> humans = new ArrayList<>();
{
  humas.add(new Stunet(...));
  humans.add(new Proffessor(....));
}

当我查看人员列表时,我想获取学生的通信地址

这是我尝试过的

for(Student student : humans){
   student.get(comminicationAddress)
}

如何通过循环人类列表来获取学生的属性?

标签: javaobject

解决方案


推荐阅读