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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© zl78365336 中级黑马   /  2013-12-3 13:43  /  1309 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 zl78365336 于 2013-12-3 14:13 编辑

  1. 为何这个不可以
  2. java.lang.Integer cannot be cast to java.lang.String


  3. ArrayList<Integer> list=new ArrayList<Integer>();
  4. Method method=list.getClass().getMethod("add",Object.class);
  5. String str= "添加字符串!";                 //创建字符串
  6. method.invoke(list, str);    //在list上调用add方法,用反射技术

  7. ArrayList<String> list=new ArrayList<String>();
  8. Method method=list.getClass().getMethod("add",Object.class);
  9. Integer i= new Integer(5);               
  10. method.invoke(list, i);    //在list上调用add方法,用反射技术


复制代码

4 个回复

倒序浏览
同学、问题已经解决了吗?
如果没有、就去新版28期问吧,26~27已经结束了。开班了!
http://bbs.itheima.com/forum-165-1.html

如果问题已经解决,请及时修改主题为“提问结束”。
修改主题的方法链接
http://bbs.itheima.com/thread-89313-1-1.html
如果没有解决,可能你的问题问得不够清楚。可以重新发问的哦~
回复 使用道具 举报

  1. 为何这个不可以
  2. java.lang.Integer cannot be cast to java.lang.String


  3. ArrayList<Integer> list=new ArrayList<Integer>();//你这里加了Integer的泛型
  4. Method method=list.getClass().getMethod("add",Object.class);//这里最好写Integer.class
  5. String str= "添加字符串!";                 //创建字符串
  6. method.invoke(list, str);    //在list上调用add方法,用反射技术      这里却又添加字符串当然不行

  7. ArrayList<String> list=new ArrayList<String>();   //同理,这里规定了String类型的泛型
  8. Method method=list.getClass().getMethod("add",Object.class);
  9. Integer i= new Integer(5);                //这里却又添加Integer类型,这不是张冠李戴吗
  10. method.invoke(list, i);    //在list上调用add方法,用反射技术


复制代码
回复 使用道具 举报
class  StringTest
{
       public static void main(String[] args) throws Exception
       {
               
                ArrayList<Integer> list1=new ArrayList<Integer>();
                Class cl=list1.getClass();
                Method me=cl.getMethod("add", Object.class);
                me.invoke(list1, "hello");
                System.out.println(list1.get(0));
               
                ArrayList<String> list2=new ArrayList<String>();
                Method method=list2.getClass().getMethod("add",Object.class);
                Integer i= new Integer(5);               
                method.invoke(list2, i);    //在list上调用add方法,用反射技术
                Object str=list2.get(0);
                String strf=String.valueOf(str);
                System.out.println(strf);
               
       }
}
那是Object类型的自动转换会报错,你要手动转换
Object str=list2.get(0);
                String strf=String.valueOf(str);
                System.out.println(strf);
用Object接受然后在用String.valueOf(Object obj)进行转换
回复 使用道具 举报
  1. class  StringTest
  2. {
  3.        public static void main(String[] args) throws Exception
  4.        {
  5.                
  6.                 ArrayList<Integer> list1=new ArrayList<Integer>();
  7.                 Class cl=list1.getClass();
  8.                 Method me=cl.getMethod("add", Object.class);
  9.                 me.invoke(list1, "hello");
  10.                 System.out.println(list1.get(0));
  11.                
  12.                 ArrayList<String> list2=new ArrayList<String>();
  13.                 Method method=list2.getClass().getMethod("add",Object.class);
  14.                 Integer i= new Integer(5);               
  15.                 method.invoke(list2, i);    //在list上调用add方法,用反射技术
  16.                 Object str=list2.get(0);
  17.                 String strf=String.valueOf(str);
  18.                 System.out.println(strf);
  19.                
  20.        }
  21. }
复制代码

那是Object类型的自动转换会报错,你要手动转换
  1. Object str=list2.get(0);
  2.                 String strf=String.valueOf(str);
  3.                 System.out.println(strf);
复制代码

用Object接受然后在用String.valueOf(Object obj)进行转换
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马