如何在不强制转换代理类型的情况下在资源单元上使用 .getServicedEntity() 方法? [英] How can I use the .getServicedEntity() method on a Resource Unit without casting the agent type?
问题描述
我构建了一个自定义块,除其他外,它告诉进入块的 portIn 的资源移动到资源的获取单元.我使用 moveTo 块和目标节点: (Node)((Cart)agent).getServicedEntity().getNetworkNode();
但我需要知道代理键入并将其转换为方法.我想让它成为一个通用块,可以在其他模型和任何资源单元中使用.
I built a custom block that, among other things, tells the resource entering portIn of the block to move to the resource's seizing unit. I use a moveTo block with the destination node as: (Node)((Cart)agent).getServicedEntity().getNetworkNode();
but I need to know the agent type and cast it into the method. I would like to make this a generic block that can be used in other models and with any Resource Unit.
我尝试在自定义块中使用通用参数
I've tried using generic parameters in the custom block
然后选择代理类型:
然后尝试:(Node)T.getServicedEntity().getNetworkNode();
但这会导致编译错误:getServicedEntity() 方法未定义为类型 T. 有没有办法做我想做的事?如果用户必须使用通用参数下拉列表选择代理类型,我很好,但我想避免每次都必须更改代码以使用 添加模型中可用的所有资源类型instanceof 命令,然后复制代码.似乎效率低下.
and then trying: (Node)T.getServicedEntity().getNetworkNode();
but this results in compiling error: The method getServicedEntity() is undefined for the type T. Is there a way to do what I'm looking to do? I'm fine if the user has to select the agent type using the generic parameter pulldown, but I'd like to avoid having to change the code every time to add in all of the resource types available in the model using the instanceof command and then duplicating the code. Seems inefficient.
推荐答案
好吧,你的T"扩展了 Agent
,而 Agent
不知道 getServicedEntity代码>.
Well, your "T" extends Agent
, and Agent
does not know about getServicedEntity
.
你的 T 扩展需要知道它是一个资源单元.我能看到的最简单的解决方案:
What your T extends needs to know it is a resource unit. Easiest solution I can see:
- 创建一个父类
MyResourceUnit
(但不要实例化它) - 确保它在流程图中用作资源单元"
- 制作所有应该使用自定义块的代理类型来扩展
MyResourceUnit
- 现在在您的自定义块中,您应该使
T extends MyResourceUnit
- Create a parent class
MyResourceUnit
(but do not instantiate it) - Make sure it is "used in flowcharts as Resource Unit"
- Make all agent types that should ever use your custom block to extend
MyResourceUnit
- Now in your custom block, you should make
T extends MyResourceUnit
这篇关于如何在不强制转换代理类型的情况下在资源单元上使用 .getServicedEntity() 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!