从Method.invoke传递的变量不会显示在除System.out.println之外的任何内容中

时间:2016-10-03 22:40:20

标签: java methods invoke

我想要做的是在游戏的输入框中创建我在屏幕上显示的内容,例如

  

"名称:(输入将显示在此处)"

我所做的是传递消息,我想要输入的对象,以及将使用该变量的函数(例如将输入设置为播放器名称的函数)。现在,我可以从InputBox对象中访问输入变量,但从长远来看,这将更有效。我使用Method.invoke将函数传递给我想要的对象,输入字符串作为参数。这是我的代码:

public class InputBox extends Textbox {

    public String input = "";
    private String out;
    private Object obj;

    private int lastLastKey = 0;

    /** 
     * 
     * @param message
     * @param cl The object/class where the output variable is.
     * @param out The method to run. Only takes the input variable, so use this format: void toRun(String input)
     */
    public InputBox(String message, Object obj, String out) {
        super(message);
        this.out = out;
        this.obj = obj;
    }

    public void tick(Game game){                
        if (game.input.lastKey != 0 && !game.input.keys[KeyEvent.VK_ENTER]
                && !game.input.keys[KeyEvent.VK_SPACE] && !game.input.keys[KeyEvent.VK_SHIFT] && !game.input.keys[KeyEvent.VK_BACK_SLASH]){
            if(game.input.lastKey != lastLastKey) input+=Character.toUpperCase((char) game.input.lastKey);
        }else if(game.input.keys[KeyEvent.VK_ENTER] && !input.isEmpty() && !cleared){
            Method method = null;
            try {
                method = obj.getClass().getMethod(out, new Class[]{ String.class, Object.class });
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (SecurityException e) {
                e.printStackTrace();
            }

            try {
                method.invoke(obj, new Object[]{ input, obj });
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            cleared = true;
        }

        lastLastKey = game.input.lastKey;
    }

    public void render(Graphics g){
        if(!cleared){
            g.setColor(Color.white);
            g.fillRect(10, Display.height-110, Display.width-20, 100);

            g.setColor(Color.black);
            g.drawRect(10, Display.height-110, Display.width-20, 100);

            g.setFont(Game.smallFont);
            FontMetrics fm = g.getFontMetrics();
            g.drawString(message+" "+input, Display.width/2-fm.stringWidth(message+" "+input)/2, Display.height-50);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

您对构造函数的描述表明预期的方法具有

的签名
void toRun(String input) {}

但您的反射代码会查找带有签名的方法

void toRun(String input, Object o) {}

如果您使用的是Java 8,则可以使用Consumer并删除所有反射代码:

public class InputBox extends Textbox {

    private Consumer<String> out;

    public InputBox(String message, Consumer<String> out) {
        this.out = out;
        ...
    }

    public void tick(Game game){
        ...
        out.apply(input);
        ...
    }
}

假设您的Player类的方法setName(String)是这样的:

public class Player {
    public void setName(String name) {
    }
}

您可以使用方法参考

创建InputBox
public InputBox createNameInputBox(Player p) {
    return new InputBox("Name: ", p::setName);
}

或使用lambda表达式

public InputBox createAlternateNameInputBox(Player p) {
    return new InputBox("Name: ", name -> p.setName(name));
}

要阅读有关lambda表达式和方法引用的更多信息,请参阅Java Tutorial系列(https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html)。有关是否应使用方法引用或lambda表达式的讨论,请参阅https://stackoverflow.com/a/24493905/5646962