简体   繁体   English

当Parmae​​ter是另一个类的实例时调用方法-Java

[英]Invoking Method when Parmaeter is instance of another class - Java

Ill try to make it short. 虐待尝试使它简短。 I want to iterate through the methods of a class via "getMethods()". 我想通过“ getMethods()”遍历类的方法。 Now when the method has a Parameter which is an instance of the "Event" class, it should be invoked. 现在,当该方法具有作为“ Event”类的实例的Parameter时,应将其调用。

Example Method: 示例方法:

private void onEvent(ExampleEvent event) {...]

"ExampleEvent" is an instance of the "Event" class. “ ExampleEvent”是“ Event”类的实例。

Help is appreciated! 感谢帮助!

You want to use getParameterTypes() instead of getParameters() . 您要使用getParameterTypes()而不是getParameters() If you want to match a parameter type to a parameter name, simply index the two arrays the same. 如果要将参数类型与参数名称匹配,只需将两个数组索引相同即可。

Something Like this, its been awhile since i wrote in java but This something like this should work

Method[] methods = Class.getMethods();
    List<Method> methodsWithEvent = new List<Method>();
    for(Method method : methods){
       Class[] params = method.getParameterTypes();
       for(Class class : params)
          if(typeof(class) == typeof(ExampleEvent))
              methodsWithEvents.add(method);

    }

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

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