定义一个类,通过反射机制来set参数值public class Dept { private String deptCode; public String getDeptCode() { return deptCode; } public void setDeptCode(String deptCode) { this.deptCode = deptCode; } public static void main(String[] args) { Dept dept = new Dept(); Class c = dept.getClass(); try { Method setDepartmentCode = c.getMethod("setDeptCode",String.class); // 根据方法名称,参数类型获取到方法对象 setDepartmentCode.invoke(dept,"2000"); // 第一个参数为类对象,后面的值为参数值,没有参数是为null System.out.println(dept.getDeptCode()); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } }}