有一个 Java 类,其中一个字段是一个方法?
我对 Java 还很陌生,因为我的帖子的性质会泄露
我需要创建一个类,其中包含一组可以由程序员轻松扩展的方法(如果需要)。我考虑过有两个类:Commands
和 Command
。 Commands
包含一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为您想使用命令模式:
维基百科页面有一个例子。
您的
Command
应该是一个带有execute
方法的接口。程序员可以编写一个实现Command
接口的类,因此她/他必须实现execute
方法来执行所需的操作。然后,您可以拥有一个 Command[] 数组,并且对于每个 Command 对象,您可以简单地调用execute 来执行任务。I think you want to use the Command pattern:
The Wikipedia page has an example.
Your
Command
should be an interface with anexecute
method. A programmer can code a class that implements theCommand
interface, and thus she/he must implement theexecute
method to do what is needed. You can then have aCommand[]
array, and for eachCommand
object you can simply callexecute
to perform the task.Java没有函数指针。这是一个常见的技巧:
Java does not have function pointers. This is a common trick:
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.请注意,您不能直接在 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.