考虑委托(delegate)中的方法
Public void findThisMethod(String a,Set b,HashSet c){}
因此,从操作 am gng 映射此方法与方法 id 和参数类型..通过使用。
Object[] arguments = new Object[] {a,b,c}
for (int i = 0 ; i < arguments.length ; i++){
argumentTypes[i] = arguments[i].getClass();
}
Method invokeMethod = delegateClass.getDeclaredMethod(methodID,argumentTypes);
returnObj = invokeMethod.invoke(delegateObj,arguments);
...问题::这不是问题,因为 Set 是一个接口(interface)!谁能告诉我如何正确映射接口(interface)???
最佳答案
如果您正在使用 getDeclaredMethod 查找方法的签名,那么您应该提供此签名的参数的确切类型,而不是子类。子类化将由编译器解决,但它对内省(introspection)没有帮助。
明确提供参数的类型,不要从对象中获取它们。你应该有类似的东西
delegateClass.getDeclaredMethod(methodID, new Class[] { String.class, Set.class, HashSet.class } );
关于java - 如何映射具有接口(interface)和类的方法——从操作到委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9938320/