내 게시물의 성격이 떨어져 버릴 것입니다.필드 중 하나가 메소드 인 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 클래스에 정의 될 것입니다. – Andrew