有没有一种简单的方法可以从超类对象列表中创建子类对象列表?
Is there a simple way to create a list of subclass objects from a list of the superclass objects?
我有一组 Agent 对象(超类)。
代理对象可以是:1) 感染(扩展代理)和 2) 健康(扩展代理)。示例...
public class Healthy extends Agent
public class Infected extends Agent
每个 Agent x 对象都保留一个列表,其中列出了与 Agent x 接触过的所有 Agent y 对象,而不管其子类。列表的类型是 Agent,这个列表是一个名为 "links" 的实例变量。示例...
public class Agent {
protected Context<Object> context;
protected Geography<Object> geog;
private int Id;
public Coordinate location;
public ArrayList<Agent> links = new ArrayList<>();
public ArrayList<Healthy> healthy_links = new ArrayList<>();
public Agent(Context<Object> context, Geography<Object> geog) {
this.context = context;
this.geog = geog;
this.Id = Id;
this.location = location;
this.links = links;
this.healthy_links = healthy_links;
}
}
//getters and setters
public void findContacts(){
Context context = ContextUtils.getContext(this);
//create a list of all agents
IndexedIterable<Agent> agents = context.getObjects(Agent.class);
for(Agent a: agents){
//if any of the agents are in the same location as this, if the links list doesnt already contain the agent, and if the agent is not this, then add it to the links list
if(a.getLocation()== this.getLocation() && !this.links.contains(a) && this != a){
this.links.add(a); //this is obviously possible//
this.healthy_links.add(a); //this is obviously not possible, but is there a super simple alternative
}
}
}
是否有一种简单的方法来遍历 Agent y 对象列表并将所有健康的 Agent 分类到名为 "healthy_links" 的 Healthy 类型的新列表中?
if (a instanceof HealthyAgent) {
this.healthy_links.add((HealthyAgent)a);
}
我有一组 Agent 对象(超类)。
代理对象可以是:1) 感染(扩展代理)和 2) 健康(扩展代理)。示例...
public class Healthy extends Agent
public class Infected extends Agent
每个 Agent x 对象都保留一个列表,其中列出了与 Agent x 接触过的所有 Agent y 对象,而不管其子类。列表的类型是 Agent,这个列表是一个名为 "links" 的实例变量。示例...
public class Agent {
protected Context<Object> context;
protected Geography<Object> geog;
private int Id;
public Coordinate location;
public ArrayList<Agent> links = new ArrayList<>();
public ArrayList<Healthy> healthy_links = new ArrayList<>();
public Agent(Context<Object> context, Geography<Object> geog) {
this.context = context;
this.geog = geog;
this.Id = Id;
this.location = location;
this.links = links;
this.healthy_links = healthy_links;
}
}
//getters and setters
public void findContacts(){
Context context = ContextUtils.getContext(this);
//create a list of all agents
IndexedIterable<Agent> agents = context.getObjects(Agent.class);
for(Agent a: agents){
//if any of the agents are in the same location as this, if the links list doesnt already contain the agent, and if the agent is not this, then add it to the links list
if(a.getLocation()== this.getLocation() && !this.links.contains(a) && this != a){
this.links.add(a); //this is obviously possible//
this.healthy_links.add(a); //this is obviously not possible, but is there a super simple alternative
}
}
}
是否有一种简单的方法来遍历 Agent y 对象列表并将所有健康的 Agent 分类到名为 "healthy_links" 的 Healthy 类型的新列表中?
if (a instanceof HealthyAgent) {
this.healthy_links.add((HealthyAgent)a);
}