Java - Creating an array of methods

前端 未结 6 2056
温柔的废话
温柔的废话 2020-12-01 01:47

I\'m designing a text-based adventure game for a school progress. I have each \"level\" set up as a class, and each explorable area (node) as a method within the appropriate

相关标签:
6条回答
  • 2020-12-01 02:05

    Whenever you think of pointer-to-function, you translate to Java by using the Adapter pattern (or a variation). It would be something like this:

    public class Node {
        ...
        public void goNorth() { ... }
        public void goSouth() { ... }
        public void goEast() { ... }
        public void goWest() { ... }
    
        interface MoveAction {
            void move();
        }
    
        private MoveAction[] moveActions = new MoveAction[] {
            new MoveAction() { public void move() { goNorth(); } },
            new MoveAction() { public void move() { goSouth(); } },
            new MoveAction() { public void move() { goEast(); } },
            new MoveAction() { public void move() { goWest(); } },
        };
    
        public void move(int index) {
            moveActions[index].move();
        }
    }
    
    0 讨论(0)
  • 2020-12-01 02:05

    Try thinking about solutions without reflection. It's can be enums, for example.

    0 讨论(0)
  • 2020-12-01 02:05

    You can use Reflection class to create an array of methods. http://java.sun.com/developer/technicalArticles/ALT/Reflection/

    0 讨论(0)
  • 2020-12-01 02:19

    Since Java does not have the concept of methods as first-class entities, this is only possible using reflection, which is painful and error-prone.

    The best approximation would probably be to have the levels as enums with a per-instance implementation of a method:

    public enum Level1 implements Explorable{
        ROOM1 {
            public void explore() {
                // fight monster
            }
        }, ROOM2 {
            public void explore() {
                // solve riddle
            }
        }, ROOM3 {
            public void explore() {
                // rescue maiden
            }
        };
    
    }
    
    public interface Explorable{
        public abstract void explore();    
    }
    
    public static void move(Explorable[] adjacentNodes, int index)
    {
        adjacentNodes[index].explore();
    }
    

    However, this is a bit of an abuse of the enum concept. I wouldn't use it for a serious project.

    0 讨论(0)
  • 2020-12-01 02:23

    Your design has fundamental flaws. Normal OO design would have each "level" be an object (of Class 'level' or something like it). each 'explorable area' would also be an object, contained within the level object - maybe of class ExplorableArea. The 'explorable areas' can be different kinds, in which case you make them different subclasses of ExplorableArea.

    0 讨论(0)
  • 2020-12-01 02:25

    Just have your nodes be objects that all adhere to the same interface, then you'll be able to call their methods reliably.

    0 讨论(0)
提交回复
热议问题