MyTest.java 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. package J20250807;
  2. import java.lang.reflect.Method;
  3. /**
  4. * @author WanJl
  5. * @version 1.0
  6. * @title MyTest
  7. * @description
  8. * @create 2025/8/7
  9. */
  10. public class MyTest {
  11. public static void main(String[] args) throws Exception{
  12. User user =new User();
  13. //创建代理对象GetterSetterProxy
  14. GetterSetterProxy proxy=new GetterSetterProxy();
  15. User userProxy = (User) proxy.createProxy(user);
  16. //通过代理对象给User对象设置属性值
  17. Method setName = userProxy.getClass().getMethod("setName", String.class);
  18. setName.invoke(userProxy,"张三");
  19. Method setAge = userProxy.getClass().getMethod("setAge", String.class);
  20. setAge.invoke(userProxy,25);
  21. //通过代理对象获取属性值
  22. Method getName = userProxy.getClass().getMethod("getName");
  23. System.out.println("姓名:"+getName.invoke(userProxy));
  24. Method getAge = userProxy.getClass().getMethod("getAge");
  25. System.out.println("年龄:"+getAge.invoke(userProxy));
  26. }
  27. }