public class TestReflection { /** * Reflect test * */ public static void main(String[] args) { ReportObject rObject = new ReportObject(); Class rObjectClass = rObject.getClass(); try { Method method = rObjectClass.getDeclaredMethod("setT1", String.class); method.invoke(rObject,"Set value here...."); System.out.println("rObject.T1 change="+rObject.getT1()); // 检测执行结果 } catch (Exception e) { e.printStackTrace(); } } } public class ReportObject { //Products attribute String t1; public String getT1() { return t1; } public void setT1(String t1) { this.t1 = t1; } }
时间: 2024-11-05 19:03:58