【发布时间】:2019-06-18 03:32:58
【问题描述】:
我知道我可以通过将整个过程中的代理类型更改为通用Agent 来在同一块中插入不同类型的代理(感谢 Amy)。 snapshot
但是,我被困在如何使用手动铸造过程selectOutputIn1 将它们取出来。每个代理类型都有一个名为p_new_location 的参数,其类型为SelectOutputOut。
我需要什么帮助
如果通过selectOutputIn1 正确转换,则传递等效于agent.p_new_location 的内容。
我尝试过的
使用手动转换创建函数:
if( agent instanceof Wife){
return ((Wife)agent).p_new_location ;
} else if(agent instanceof Child_M){
return ((Child_M)agent).p_new_location ;
} else if(agent instanceof Child_F){
return ((Child_F)agent).p_new_location ;}
不幸的是,正如您所见,虽然方法已经定义,但它必须返回 SelectOutputOut 类型的结果。
每个类中的参数类型与this 完全一样。从模拟内部看,它在传递值之前看起来像this,在传递值之后看起来像this。此外,我注意到像SelectOutputOut 这样不常见的参数类型的值在模拟过程中没有显示 as shown here。正如你所看到的,参数(age、countDb 和 taken)都有它们的值,但不是p_new_location。
解决方案
再次感谢艾米 :)
编译器正在查看 if / else if / else if。如果这些都不是真的呢?如果您的最后一个 else if 是唯一的选择,只需将其更改为 else 或为您想做的事情输入适当的代码。
这就是代码现在的样子
if( agent instanceof Wife){
return ((Wife)agent).p_new_location ;
} else if(agent instanceof Child_M){
return ((Child_M)agent).p_new_location ;
} else{
return ((Child_F)agent).p_new_location ;}
提前致谢;
【问题讨论】:
-
请说明您在建议的代码中实际遇到的问题。
-
艾米·格里尔在上面有很好的观点。正如她所说,您需要强制执行操作才能访问特定属性。在执行此操作之前,您需要检查对象类型。您可能知道这一点,但为了其他人阅读此代码,代码类似于。 if (agent instanceof Wife) { ( ( Wife ) agent ).aWifeFunctionCall(); }
-
感谢您的帮助,您真好。我现在面临一个新问题。希望不是错误的铸造方法。
标签: anylogic