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

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

问题描述

我构建了一个自定义块,除其他外,该块告诉资源输入该块的 portIn 到资源的捕获单元.我使用目标节点为 moveTo 块,但我需要知道代理类型并将其转换为方法.我想使它成为可以在其他模型和任何资源单元中使用的通用块.

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();,但是这会导致编译错误:对于类型T,未定义方法getServicedEntity().有什么方法可以做我想要做的事情?我很好,如果用户必须使用通用参数下拉菜单选择代理类型,但我想避免每次都要更改代码以使用添加模型中可用的所有资源类型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天全站免登陆