反射方法调用以及自动根据方法参数类型和个数进行方法调用——反射_4 – 热爱改变生活
我的GitHub GitHub |     登录
  • If you can't fly, then run; if you can't run, then walk; if you can't walk, then crawl
  • but whatever you do, you have to keep moving forward。
  • “你骗得了我有什么用,这是你自己的人生”
  • 曾有伤心之地,入梦如听 此歌

反射方法调用以及自动根据方法参数类型和个数进行方法调用——反射_4

Java sinvader 3523℃ 0评论

首先放出测试类

  1. class Math_Test {
  2. public void add(int a, int b) {
  3. System.out.println(a + b);
  4. }
  5.  
  6. public int add(int a, int b, int c) {
  7. return a + b + c;
  8. }
  9.  
  10. public void add(String a, String b) {
  11. int a_ = Integer.parseInt(a);
  12. int b_ = Integer.parseInt(b);
  13. System.out.println(a_ + b_);
  14. }
  15. }

在主类中进行测试

  1. public static void main(String[] args) {
  2. Math_Test m = new Math_Test();
  3. // 首先获得 m 的类类型
  4. Class c = m.getClass();
  5. // use_add_int_int(m, c);
  6. // use_add_int_int_int(m, c);
  7. // use_add_string_string(m, c);
  8. // 获得自定以的方法
  9. Method[] method = c.getDeclaredMethods();
  10. for (Method method2 : method) {
  11. System.out.print("当前调用的方法:" + method2.getName() + "(");
  12. // 获取参数类型
  13. Class[] type = method2.getParameterTypes();
  14. String parType = "";
  15. for (Class class1 : type) {
  16. System.out.print(class1.getSimpleName() + ",");
  17. // 记录参数类型个数以及分别是什么类型的
  18. if ((class1.getName()).contains("String")) {
  19. parType += "s";
  20. }
  21. if ((class1.getName()).contains("int")) {
  22. parType += "i";
  23. }
  24. }
  25. System.out.print(")");
  26. System.out.println();
  27. // 根据记录的参数类型以及个数 invoke 不同的方法
  28. if (parType.equals("ss")) {
  29. try {
  30. Method method3 = c.getMethod(method2.getName(), String.class, String.class);
  31. System.out.print("传入参数:" + "'2'+'3'" + "结果为:");
  32. method3.invoke(m, "2", "3");
  33. } catch (Exception e) {
  34. // TODO Auto-generated catch block
  35. e.printStackTrace();
  36. }
  37. } else if (parType.equals("ii")) {
  38. try {
  39. Method method3 = c.getMethod(method2.getName(), int.class, int.class);
  40. System.out.print("传入参数:" + "1+2" + "结果为:");
  41. method3.invoke(m, 1, 2);
  42. } catch (Exception e) {
  43. // TODO Auto-generated catch block
  44. e.printStackTrace();
  45. }
  46. } else if (parType.equals("iii")) {
  47. try {
  48. Method method3 = c.getMethod(method2.getName(), int.class, int.class, int.class);
  49. System.out.print("传入参数:" + "1+2+3" + "结果为:");
  50. Object o = method3.invoke(m, 1, 2, 3);
  51. System.out.println((int) o);
  52. } catch (Exception e) {
  53. // TODO Auto-generated catch block
  54. e.printStackTrace();
  55. }
  56. }
  57. }
  58. }

以上代码运行结果:

  1. 当前调用的方法:add(String,String,)
  2. 传入参数:'2'+'3'结果为:5
  3. 当前调用的方法:add(int,int,int,)
  4. 传入参数:1+2+3 结果为:6
  5. 当前调用的方法:add(int,int,)
  6. 传入参数:1+2 结果为:3

三个被注释掉的普通方式下的方法调用

  1. private static void use_add_string_string(Math_Test m, Class c) {
  2. try {
  3. Method me = c.getMethod("add", String.class, String.class);
  4. me.invoke(m, "1", "2");
  5. } catch (Exception e) {
  6. // TODO Auto-generated catch block
  7. e.printStackTrace();
  8. }
  9. }
  10. private static void use_add_int_int_int(Math_Test m, Class c) {
  11. // 获得方法名为"add",参数列表为 (int,int) 类型的方法
  12. try {
  13. Method me;
  14. me = c.getMethod("add", int.class, int.class, int.class);
  15. Object o = me.invoke(m, 2, 3, 1);
  16. System.out.println("通过返回值获得的结果:" + (int) o);
  17. } catch (Exception e) {
  18. // TODO Auto-generated catch block
  19. e.printStackTrace();
  20. }
  21. }
  22. private static void use_add_int_int(Math_Test m, Class c) {
  23. try {
  24. // 获得方法名为"add",参数列表为 (int,int) 类型的方法
  25. Method me = c.getMethod("add", int.class, int.class);
  26. me.invoke(m, 2, 3);
  27. } catch (Exception e) {
  28. // TODO Auto-generated catch block
  29. e.printStackTrace();
  30. }
  31. }
本文测试代码下载
¥ 有帮助么?打赏一下~

转载请注明:热爱改变生活.cn » 反射方法调用以及自动根据方法参数类型和个数进行方法调用——反射_4


本博客只要没有注明“转”,那么均为原创。 转载请注明链接:sumile.cn » 反射方法调用以及自动根据方法参数类型和个数进行方法调用——反射_4

喜欢 (1)
发表我的评论
取消评论
表情

如需邮件形式接收回复,请注册登录

Hi,你需要填写昵称和邮箱~

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址