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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 沈雷 黑马帝   /  2011-8-4 16:17  /  1453 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

[code=java]import java.lang.reflect.Method;

import org.junit.Test;

public class People {
        public void printNum1 ( String[] nums){
                for(String num : nums){
                        System.out.println(num);
                }
               
        }
        public void printNum2 ( int[] nums){
                for(int num : nums){
                        System.out.println(num);
                }
               
        }
        @Test
        public void test1() throws Exception{
                Class clazz = People.class;
                Method method = clazz.getMethod("printNum1", String[].class);
                method.invoke(new People(), new String[]{"hello","word"});
        }
        @Test
        public void test2() throws Exception{
                Class clazz = People.class;
                Method method = clazz.getMethod("printNum2", int[].class);
                method.invoke(new People(), new int[]{1,2});
        }

}[/code]questions:test1有问题,test2正常。这是为什么?
[ 本帖最后由 沈雷 于 2011-08-04  16:22 编辑 ]

1 个回复

倒序浏览
黑马网友  发表于 2011-8-4 16:28:31
沙发
[code]Method method = clazz.getMethod("printNum", String[].class);[/code][color=Red]printNum[/color] 方法需要传递整形数组,你传字符串数组了。。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马