// 根据类名和方法名执行方法 beanClass:全路径 methodName:方法名,要执行的方法
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void executeMethod(String beanClass, String methodName) {
try {
Class clz = Class.forName(beanClass);
Method method = clz.getMethod(methodName);
//调用方法
method.invoke(clz.newInstance());
} catch (Exception e) {
e.printStackTrace();
}
}
// 属性赋值,前提是该属性有setter方法。value:赋的值
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void setValue(String beanClass, Integer value) {
try {
Class clz = Class.forName(beanClass);
Method setDT = clz.getMethod("setDT", Integer.class);
setDT.invoke(clz, Integer.valueOf(value));
} catch (Exception e) {
e.printStackTrace();
}
}
根据反射执行某一个类的方法
最新推荐文章于 2025-10-30 18:31:30 发布
本文介绍了如何使用Java反射机制执行特定类的方法及为属性赋值。通过反射,可以在运行时动态地获取类信息并操作类的方法和属性。文章提供了具体的代码实现,包括执行指定方法和设置属性值的过程。
1083

被折叠的 条评论
为什么被折叠?



