有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

反射Java getMethod()会导致NoSuchMethodException错误

我试图在用户输入字符串命令时使用反射来启动适当的方法。例如,如果用户在终端中输入“go”,则命令类的process()方法将调用Player类的go()方法

然而,我无法让我的代码工作,我得到了一个我不知道如何修复的NoSuchMethodException错误。问题的根源是命令类的一半(在底部复制的完整类):

        try {
            Method method = pClass.getMethod(commandWord);
            method.invoke(player, this);
        }
        catch (NoSuchMethodException err1) {
            System.out.println("No such method");
        }

谁能给我指路吗?我先谢谢你

信用证

命令类:

import java.util.ArrayList;
import java.lang.reflect.*;

public class Command
{

    private String commandWord;
    private String secondWord;

    public Command(String firstWord, String secondWord)
    {
        commandWord = firstWord;
        this.secondWord = secondWord;
    }

    public boolean process(Player player) 
    {
        ArrayList<String> validCommands = new ArrayList<String>();
        String methodName;
        int index;

        Class pClass = player.getClass();
        Method[] methods = pClass.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            if (Modifier.isPublic(methods[i].getModifiers())) {
                validCommands.add(methods[i].getName());
            }
        }

        boolean wantToQuit = false;

        if(commandWord == null) {
            System.out.println("I don't know what you mean...");
            return false;
        }

        if (commandWord.equals("help")) {
            System.out.println("You are lost. You are alone. You wander");
            System.out.println("around at the university.");
            System.out.println();
            System.out.println("Your command words are:");

            for(String command: validCommands) {
                System.out.print(command + "  ");
            }
            System.out.println();
        }
        else if (commandWord.equals("quit")) {
            wantToQuit = quit();
        }

        //THIS IS WHERE I GET THE ERROR
        try {
            Method method = pClass.getMethod(commandWord);
            method.invoke(player, this);
        }
        catch (NoSuchMethodException err1) {
            System.out.println("No such method");
        }
        catch (IllegalAccessException err2) {
            System.out.println("Illegal access");
        }
        catch (InvocationTargetException err3) {
            System.out.println("Illegal access");
        }

        return wantToQuit;
    }

    [...] //some methods omitted
}

玩家等级:

public class Player
{
    private String name;
    private Room currentRoom;
    private ArrayList<Item> items;

    Player (String name, Room startingRoom)
    {
        this.name = name;
        items = new ArrayList<Item>();
        this.currentRoom = startingRoom;
        printWelcome();
    }

    public void engage()
    {
        [...]
    }

    public void trade(Command command) 
    {
        [...]       }

    public void goRoom(Command command) 
    {   
        [...]       }

    public void search(Command command) 
    {
        [...]       }

    public void takeItem(Command command) 
    {
        [...]       }

    public void dropItem(Command command) 
    {
        [...]       }

    public void lock(Command command)
    {   
        [...]       }

    public void unlock(Command command)
    {   
        [...]
    }

}

共 (1) 个答案

  1. # 1 楼答案

    试试这个

    pClass.getMethod(commandWord, Command.class)

    在我看来,问题在于你正在寻找没有
    参数,而这些方法似乎都有一个类型为Command的参数

    有关更多详细信息,请参见此处:

    Class.getMethod JavaDoc