How can I use the .getServicedEntity() method on a Resource Unit without casting the agent type?

半腔热情 提交于 2020-05-17 08:49:16

问题


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

then selecting the agent type:

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.


回答1:


Well, your "T" extends Agent, and Agent does not know about getServicedEntity.

What your T extends needs to know it is a resource unit. Easiest solution I can see:

  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


来源:https://stackoverflow.com/questions/61684947/how-can-i-use-the-getservicedentity-method-on-a-resource-unit-without-casting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!