【发布时间】:2011-08-10 22:33:01
【问题描述】:
我对 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;
}
}
我知道这有很多问题,但我一直在寻找正确的方法。提示/帮助会很棒。
【问题讨论】:
-
你想通过这个实现的总体目标是什么?
-
会有很多命令,用户可以通过输入“execute name_of_command”来执行。命令将在 Commands 类中定义
标签: java class object methods field