我对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;
}
}
我知道有很多事情错,但我卡上找到合适的方式。提示/帮助将是太棒了。
你试图用这个来实现的总体目标是什么? – 2011-04-22 11:57:30
会有很多命令,用户可以通过输入“execute name_of_command”来执行它们。命令将在命令类 – Andrew 2011-04-22 14:47:23