A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© wsssx   /  2011-12-31 17:40  /  1676 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

提示: 作者被禁止或删除 内容自动屏蔽

3 个回复

倒序浏览
  1. import java.lang.reflect.Method;

  2. public class InvokeTester
  3. {
  4.         public int add(int param1,int param2)
  5.         {
  6.                 return param1+param2 ;
  7.         }
  8.        
  9.         public String echo(String message)
  10.         {
  11.                 return "hello "+message;
  12.                
  13.         }
  14.        
  15.         public static void main(String[] args) throws Exception
  16.         {
  17. //                InvokeTester a = new InvokeTester();
  18. //               
  19. //                System.out.println(a.add(2, 3));
  20.                
  21.                 Class<?> classType = InvokeTester.class;
  22.                
  23.                 Object invokeTester = classType.newInstance();
  24.                
  25. //                System.out.println(invokeTester instanceof InvokeTester);
  26.                
  27.                 Method addMethod = classType.getMethod("add",new Class[]{Integer.TYPE,Integer.TYPE});
  28.                
  29.                 Object result = addMethod.invoke(invokeTester, new Object[]{1,2});
  30.                
  31.                 System.out.println((Integer)result);
  32.                
  33.                 System.out.println("-------------------------------------------------------");
  34.                
  35.                 Method echoMethod = classType.getMethod("echo", new Class[]{String.class});
  36.                 Object result2 = echoMethod .invoke(invokeTester, new Object[]{"tom"});
  37.                
  38.                 System.out.println((String)result2);
  39.                
  40.                
  41.         }
  42. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
杨强 + 1

查看全部评分

回复 使用道具 举报
看一下我这个程序吧,应该明显了
回复 使用道具 举报
应该是自动装箱了,从int转为nteger
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马