Java - 创建一个方法数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4280727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:20:02  来源:igfitidea点击:

Java - Creating an array of methods

javaarraysmethods

提问by Jason Baker

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 class.

我正在为学校的进步设计一个基于文本的冒险游戏。我将每个“级别”设置为一个类,并将每个可探索区域(节点)设置为相应类中的一个方法。

What's messing with me is the code to move from one node to another. Because each node is connected to up to four other nodes, I have to repeat an extremely similar block of code in each method.

困扰我的是从一个节点移动到另一个节点的代码。因为每个节点最多连接到四个其他节点,所以我必须在每个方法中重复一个极其相似的代码块。

What I'd prefer to do is include an array of methods at the beginning of each node, like this:

我更喜欢在每个节点的开头包含一组方法,如下所示:

public static void zero()
{
    ... adjacentNodes[] = {one(), two(), three(), four()};
}

And then send that array to a generic method, and have it send the player to the right node:

然后将该数组发送到通用方法,并让它将播放器发送到正确的节点:

public static void move(...[] adjacentNodes, int index)
{
    adjacentNodes[index];
}

I simplified my code, but that's the general idea. Is this possible?

我简化了我的代码,但这是一般的想法。这可能吗?

采纳答案by gpeche

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:

每当您想到函数指针时,您都会使用适配器模式(或变体)转换为 Java。它会是这样的:

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();
    }
}

回答by bhavinp

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

您可以使用 Reflection 类来创建方法数组。 http://java.sun.com/developer/technicalArticles/ALT/Reflection/

回答by Fredrick Pennachi

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

只需让您的节点成为所有遵循相同接口的对象,然后您就可以可靠地调用它们的方法。

回答by Stan Kurilin

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

尝试不加思考地思考解决方案。例如,它可以是enums

回答by DJClayworth

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.

您的设计存在根本缺陷。正常的 OO 设计会让每个“级别”都是一个对象(属于“级别”类或类似的东西)。每个“可探索区域”也将是一个对象,包含在关卡对象中 - 可能属于 ExplorableArea 类。“可探索区域”可以是不同的种类,在这种情况下,您可以将它们设为 ExplorableArea 的不同子类。

回答by Michael Borgwardt

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.

由于 Java 没有将方法作为一等实体的概念,因此只能使用反射,这是痛苦且容易出错的。

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.

然而,这有点滥用枚举概念。我不会将它用于严肃的项目。