有一个 Java 类,其中一个字段是一个方法?

发布于 2024-11-02 20:54:46 字数 1111 浏览 0 评论 0原文

我对 Java 还很陌生,因为我的帖子的性质会泄露

我需要创建一个类,其中包含一组可以由程序员轻松扩展的方法(如果需要)。我考虑过有两个类:CommandsCommandCommands 包含一个 Command 对象数组,程序员可以在其中添加新命令。 Command 类有两个字段。类的名称和方法签名。我不知道如何做到这一点。在C中,我认为你可以有一个函数结构,那么我们可以有一个类,其中类的实例是方法吗?或者我完全走错了路?

我想过尝试做这样的事情:

public class Commands
{
    private ArrayList<Command> commands;

    /**
     * Constructor for objects of class Command
     */
    public Commands()
    {
        createCommands();
    }

    /**
     * This is where a programmer can add new commands
     */
    public void createCommands()
    {
        commands.add(new Command("move", public void move()));
    }

    /**
     * This is where the programmer can define the move command
     */
    public void move()
    {
        ....
    }
}

public class Command
{
    private String command_name;
    private Method command;

    public Command(String command_name, Method command)
    {
        this.command_name = command_name;
        this.command = command;
    }
}

我知道这有很多问题,但我一直在寻找正确的方法。提示/帮助会很棒。

I'm pretty new to Java, as the nature of my post will give away

I need to create a class which contains a set of methods which can easily be extended by a programmer, should it be needed. I thought about having two classes: Commands and Command. Commands contains an array of Command objects, and is where the programmer can add new commands. The Command class has two fields. The name of the class, and a method signature. I'm not sure how this can be done. In C, I think you can have a struct of functions, so can we have a class where the instances of the class are methods? Or am I completely on the wrong track?

I thought about trying to do something like this:

public class Commands
{
    private ArrayList<Command> commands;

    /**
     * Constructor for objects of class Command
     */
    public Commands()
    {
        createCommands();
    }

    /**
     * This is where a programmer can add new commands
     */
    public void createCommands()
    {
        commands.add(new Command("move", public void move()));
    }

    /**
     * This is where the programmer can define the move command
     */
    public void move()
    {
        ....
    }
}

public class Command
{
    private String command_name;
    private Method command;

    public Command(String command_name, Method command)
    {
        this.command_name = command_name;
        this.command = command;
    }
}

I know there are a lot of things wrong with this, but I'm stuck on finding the right way. Hints/help would be fantastic.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夢归不見 2024-11-09 20:54:46

我认为您想使用命令模式

在面向对象编程中,
命令模式是一种设计模式
用一个对象来表示
并封装所有信息
稍后需要调用一个方法
时间

维基百科页面有一个例子。

您的Command 应该是一个带有execute 方法的接口。程序员可以编写一个实现Command接口的类,因此她/他必须实现execute方法来执行所需的操作。然后,您可以拥有一个 Command[] 数组,并且对于每个 Command 对象,您可以简单地调用execute 来执行任务。

I think you want to use the Command pattern:

In object-oriented programming, the
command pattern is a design pattern in
which an object is used to represent
and encapsulate all the information
needed to call a method at a later
time

The Wikipedia page has an example.

Your Command should be an interface with an execute method. A programmer can code a class that implements the Command interface, and thus she/he must implement the execute method to do what is needed. You can then have a Command[] array, and for each Command object you can simply call execute to perform the task.

意中人 2024-11-09 20:54:46

Java没有函数指针。这是一个常见的技巧:

abstract class (or interface) Command {
  void execute();
}

public void createCommands() {
  commands.add(new Command(){ 
      @Override
      void execute() {
        something.move();
      }
    });
}

Java does not have function pointers. This is a common trick:

abstract class (or interface) Command {
  void execute();
}

public void createCommands() {
  commands.add(new Command(){ 
      @Override
      void execute() {
        something.move();
      }
    });
}
乙白 2024-11-09 20:54:46

Java 没有函数指针,所以这不起作用。您可能想要做的是拥有一个命令接口,其中包含您在具体子类中实现的execute() 方法。

这是风格问题,但我通常不会在命令实现中包含名称字段。相反,我只是创建一个 Map 来保存每个命令的名称。

Java doesn't have function pointers, so this doesn't work. What you probably want to do is have a Command interface with an execute() method that you implement in concrete subclasses.

It's a matter of style, but I usually wouldn't have a name field in the Command implementations. Rather, I would just create a Map<String, Command> that holds the name for each Command.

兰花执着 2024-11-09 20:54:46

请注意,您不能直接在 Java 中传递方法。相反,它们需要是实现公共接口的对象。例如,您的方法可以成为实现 Runnable 接口的对象。然后当你需要使用该方法时,只需调用“run”即可。

Note that you can't directly pass methods about in Java. Instead, they will need to be objects that implement a common interface. For example, your methods could become objects that implement the Runnable interface. Then you just call "run" when you need to use the method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文