简体   繁体   English

Java:调用具有特定类型参数的方法

[英]Java: invoke a method with specific types of parameters

Im working on an event handling system, and i am trying to make every aspect in it automated, to do that, im trying to create a method that fires event handlers in an automatic fashion, but ive hit a wall. 我正在开发一个事件处理系统,我试图使它的每个方面都自动化,以实现这一目标,我试图创建一种以自动方式触发事件处理程序的方法,但我却遇到了麻烦。

Im trying to make a method that invokes methods by their parameters, i dont care about their names, or content, just their parameter types. 我试图制作一个通过其参数调用方法的方法,我不在乎它们的名称或内容,而只是它们的参数类型。 The thing is, i dont know how to create a method that invokes certain methods by their parameter type. 问题是,我不知道如何创建通过其参数类型调用某些方法的方法。

A simple example to get you guys a better understanding of what im trying to accomplish. 一个简单的例子,可以让您更好地了解自己要完成的工作。

public void invokeEvent(Event eventToBeInvoked) {

    for(Method m : listenerClass.getMethods()) {
        for(Parameter p : m.getParameters()) {
            if(p.getClass().equals(eventToBeInvoked.getClass())) {
                m.invoke(eventToBeInvoked);
            }
        }
    }
}

listenerClass is a class that contains all the methods that handle the events when they are fired, so lets say the event that was fired in the above example is DiceRollEvent which extends the Event class, a method that receives a DiceRollEvent will be invoked by the example above, like this: listenerClass是一个类,其中包含触发事件时处理事件的所有方法,因此可以说,在上面的示例中触发的事件是DiceRollEvent,它扩展了Event类,该示例将调用接收DiceRollEvent的方法。像这样:

public void diceRollHandler(DiceRollEvent e) {
        int turnNo = e.getTurnNumber();
        System.out.println("This is the 34th turn of the game.");
}

which will result in the end output to be: "This is the 34th turn of the game.". 这将导致最终输出为:“这是游戏的第34回合。”。

This is where i've hit the wall, how do i make a class invoke methods only containing the certain parameter types? 这就是我碰壁的地方,如何使一个仅包含某些参数类型的类调用方法?

Thank you for your assistance! 谢谢您的帮助!

I'm not entirely sure what you're looking for, but it might be this (polymorphic method): 我不确定您要寻找什么,但是可能是这样(多态方法):

Inside the event handler class have a method: 在事件处理程序类中有一个方法:

public void eventHandler(Event event){
    event.handleEvent();
}

That way any class the has Event as a parent can be sent as a parameter. 这样,任何将Event作为父类的类都可以作为参数发送。 And then in the Event class make a method: 然后在Event类中创建一个方法:

public void handleEvent(){
    //Do something
}

Or you can override the above method to suit the needs of every specific class that extends from Event ( @override ). 或者,您可以重写上面的方法以适合从Event@override )扩展的每个特定类的需求。 Hope that's what you're looking for! 希望这就是您要寻找的!

Edit 编辑

Or you may be looking for something like this: 或者,您可能正在寻找这样的东西:

public void eventHandler(DiceRollEvent event){
    //Do something
}

public void eventHandler(SomeOtherEvent event){
    //Do something
}

Giving external system or user the right of invoking arbitrary method on arbitrary object could be dangerous and I would suggest building an API that would respect the objects for which you can invoke certain methods. 向外部系统或用户授予在任意对象上调用任意方法的权利可能很危险,我建议构建一个API,该API会尊重您可以为其调用某些方法的对象。

Nevertheless you can try using Reflection API. 不过,您可以尝试使用Reflection API。 It does what you explained in the code snipped of yours. 它完成了您在摘录的代码中解释的内容。

First get a Method via: 首先通过以下方法获取方法:

java.lang.Class
    public Method getDeclaredMethod(String name, Class<?>... parameterTypes)

And then invoke the Method on an instance of interest. 然后在感兴趣的实例上调用Method。

java.lang.reflect.Method
    public Object invoke(Object obj, Object... args)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM