【发布时间】:2019-12-08 19:00:06
【问题描述】:
我有如下界面:
public interface IStaff {
public StaffPosition getPosition();
public String toString();
}
和班级:
public class Worker implements IStaff {
private String name = null;
private String surname = null;
private int age = 0;
//StaffPosition is an enumeration class
private StaffPosition position= null;
public Worker (String name, String surname, int age, StaffPosition position){
this.name = name;
this.surname= surname;
this.age= age;
this.position= position;
}
@Override
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append(this.name);
buffer.append(" ");
buffer.append(this.surname);
return buffer.toString();
}
@Override
public StaffPosition getPosition() {
return this.position;
}
public int getAge(){
return this.age;
}
在另一个类 - 建筑物中,我有一个 HashMap<Office, IStaff> offices,其中 Office 是一个普通类,它只保存办公室的号码,并有一个用于该号码的吸气剂。
然后在另一个类公司中,我有一个ArrayList<Building> buildings,它包含有关公司所有建筑物的信息。在这门课上,我需要得到工人的年龄,但我该怎么做呢?到目前为止,我有一个这样的循环来到达地图:
for (Building building: buildings) {
for (Map.Entry<Office, IStaff> office: building.offices.entrySet()) {
//get the age of a worker
}
}
有没有办法做到这一点?
【问题讨论】: