Yes, you can store methods in arrays using Reflection , however it is likely that in this situation you really want to use polymorphism .
As an example of polymorphism in relation to your problem - let's say you created the interface as follows:
public interface MoveCommand { void move(); }
Then you can create an implementation as follows:
public class MoveLeftCommand implements MoveCommand { public void move() { System.out.println("LEFT"); } }
etc. for other moving options. Then you can save them in MoveCommand[] or collection, for example List<MoveCommand> , and then List<MoveCommand> iterate over the array / collection, calling move () for each element, for example:
public class Main { public static void main(String[] args) { List<MoveCommand> commands = new ArrayList<MoveCommand>(); commands.add(new MoveLeftCommand()); commands.add(new MoveRightCommand()); commands.add(new MoveLeftCommand()); for (MoveCommand command:commands) { command.move(); } } }
Polymorphism is very powerful, and the above example is a very simple example of what is called a command pattern . Enjoy the rest of your Wumpus World implementation :)
brabster
source share