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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张飞年 中级黑马   /  2012-10-4 16:41  /  1410 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 张飞年 于 2012-10-5 00:54 编辑
  1. public class PutString {
  2.         public static void main(String[] args) throws Exception{
  3.                 ArrayList<Integer> arraylist = new ArrayList<Integer>();
  4.                 arraylist.add(123);
  5.                 arraylist.add(234);
  6.                 Class clazz = PutString.class;
  7.                 Object obj = clazz.newInstance();
  8.                 Method methods = clazz.getMethod("add");//<font color="red">这里怎么取出add方法呢?老是在这出错 java.lang.NoSuchMethodException</font>
  9.                 methods.invoke(obj, "abcdefg");
  10.                 for(Integer a : arraylist){
  11.                         System.out.println(a);
  12.                 }
  13.         }
  14. }
  15. //这里想把abcdefg放进集合中
复制代码

评分

参与人数 1技术分 +1 收起 理由
刘芮铭 + 1 赞一个!

查看全部评分

4 个回复

倒序浏览
本帖最后由 黄小贝 于 2012-10-4 17:00 编辑

亲爱的~你犯了两个错误~~

第一个错误你应该懂~~粗心了~

第二个错误~~请看getMethod的函数声明~

public Method getMethod(String name, Class<?>... parameterTypes)
        throws NoSuchMethodException, SecurityException {
        // be very careful not to change the stack depth of this
        // checkMemberAccess call for security reasons
        // see java.lang.SecurityManager.checkMemberAccess
        checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
        Method method = getMethod0(name, parameterTypes);
        if (method == null) {
            throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
        }
        return method;
    }


更正后

import java.lang.reflect.Method;
import java.util.ArrayList;

public class PutString {
        public static void main(String[] args) throws Exception{
                ArrayList<Integer> arraylist = new ArrayList<Integer>();
                arraylist.add(123);
                arraylist.add(234);
                Class clazz = ArrayList.class;//错误1
                Object obj = clazz.newInstance();
                Method methods = clazz.getMethod("add",Object.class);//错误2
                methods.invoke(obj, "abcdefg");
                for(Integer a : arraylist){
                        System.out.println(a);
                }
        }
}
//这里想把abcdefg放进集合中                                                                             









评分

参与人数 1技术分 +1 收起 理由
刘芮铭 + 1 赞一个!

查看全部评分

回复 使用道具 举报
本帖最后由 覃宏海 于 2012-10-4 17:00 编辑

public class PutString {

        public static void main(String[] args) throws Exception{

                ArrayList<Integer> arraylist = new ArrayList<Integer>();

                arraylist.add(123);

                arraylist.add(234);

                Class clazz = PutString.class;

                Object obj = clazz.newInstance();

                Method methods = clazz.getMethod("add");//<font color="red">这里怎么取出add方法呢?老是在这出错 java.lang.NoSuchMethodException</font>
看来你对反射还是没看懂!你获得的是class PutString的字节码对吧!那么class PutString里有什么方法呢?它其实就只有一个main方法。而你需要的是集合中的add方法,但这个方法不是class PutString里面有的!而是ArrayList这个类里面才有!

                methods.invoke(obj, "abcdefg");

                for(Integer a : arraylist){

                        System.out.println(a);

                }

        }

}

//这里想把abcdefg放进集合中

评分

参与人数 1技术分 +1 收起 理由
刘芮铭 + 1 赞一个!

查看全部评分

回复 使用道具 举报
覃宏海 发表于 2012-10-4 16:58
public class PutString {

        public static void main(String[] args) throws Exception{

呵呵 ,多谢批评。做的头混脑涨的,就。。。就短路勒
回复 使用道具 举报
黄小贝 发表于 2012-10-4 16:56
亲爱的~你犯了两个错误~~

第一个错误你应该懂~~粗心了~

嗯 嗯,,懂了。下去还得把反射多看几遍啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马