casting - Is it possible to block/deny a cast conversion in Java? -


I have a simple game code, where an agent interface should be applied to create an agent controller In the letter gamestate is a class game game that is the gamestate interface, and it can be passed to the implementing object agent, so the agent can read and analyze the data from the game state, and the appropriate action of the agent (the form of an intern Return) Ahia agent should.

This agent is the agent that should implement agents:

  Public interface AgentInterface {// Return value joystick Specifies the direction of the public int verb GameStat Interface GS); }  

Playing the game with an agent named MyAgent:

  GameState gs = new GameState (); Agent interface agent = New MyAgent (); While (true) {// more code here int best action = agent.action (gs) // more code here}  

However, there is some information in the gameState that the agent should not be Able to enter, because it will be deceptive to the controller, but the gamestate interface will allow the agent to obtain information by performing a Cast conversion in GameState, which is not defined in the GameStateInterface, like this:

  Public MyAgent AgentInterface {public int action (GameStateInterface g S) applies {int nLives = (GameState) GS). NLivesRemaining; // Is it possible to stop this cast? // more stuff return the best action here; }}  

My question is, is it possible to stop an artist conversion? I know that polymorphism is one of the main features of Java and object-oriented programming languages, but in this way I would like to avoid to avoid conversions.

I know that it can be solved in many other ways, but I was curious to know if it is possible to do this.

Thanks in advance.

As far as I know, it is not possible to stop a typedcast and reject it (say , By throwing the classiest exposition).

But instead of trying to deny the typecapes, you control access to the real gestate object, just apply a proxy class, which only executes the gamestate interface and it's all method for gamestate object Allows for forwarding calls. Now, instead of passing the actual gestate object reference to the action method, you are wrapped it by the example of your proxy class.


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -