如何在不强制转换代理类型的情况下在资源单元上使用 .getServicedEntity() 方法? [英] How can I use the .getServicedEntity() method on a Resource Unit without casting the agent type?

查看:28
本文介绍了如何在不强制转换代理类型的情况下在资源单元上使用 .getServicedEntity() 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个自定义块,除其他外,它告诉进入块的 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:

  1. 创建一个父类MyResourceUnit(但不要实例化它)
  2. 确保它在流程图中用作资源单元"
  3. 制作所有应该使用自定义块的代理类型来扩展 MyResourceUnit
  4. 现在在您的自定义块中,您应该使 T extends MyResourceUnit
  1. Create a parent class MyResourceUnit (but do not instantiate it)
  2. Make sure it is "used in flowcharts as Resource Unit"
  3. Make all agent types that should ever use your custom block to extend MyResourceUnit
  4. Now in your custom block, you should make T extends MyResourceUnit

这篇关于如何在不强制转换代理类型的情况下在资源单元上使用 .getServicedEntity() 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆