问题
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:
- 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
来源:https://stackoverflow.com/questions/61684947/how-can-i-use-the-getservicedentity-method-on-a-resource-unit-without-casting