使用.getDeclaredMethod从类中扩展另一个方法

使用.getDeclaredMethod从类中扩展另一个方法,第1张

使用.getDeclaredMethod从类中扩展另一个方法

听起来您只需要使用

getMethod
而不是即可
getDeclaredMethod
。整个要点
getDeclaredMethod
是,它
查找在您要对其调用的类中声明的方法:

返回一个Method对象,该对象反映此Class对象表示的类或接口的指定声明方法。

鉴于

getMethod

在C中搜索任何匹配方法。如果未找到匹配方法,则对C的超类递归调用步骤1的算法。

那只会找到 公共
方法。如果您使用的方法不是公开的,则应使用

getDeclaredMethod
或递归层次结构中的
getDeclaredMethods
每个类,自行递归该类层次结构:

Class<?> clazz = plugin.getClass();while (clazz != null) {    Method[] methods = clazz.getDeclaredMethods();    for (Method method : methods) {        // Test any other things about it beyond the name...        if (method.getName().equals("getFile") && ...) { return method;        }    }    clazz = clazz.getSuperclass();}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5489936.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存