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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© a12366456 中级黑马   /  2015-7-22 00:18  /  660 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 a12366456 于 2015-7-22 00:30 编辑

复制代码
  1. class Person {
  2.         private int id;
  3.         public String name;

  4.         public void printInfo() {
  5.                 System.out.println("name=" + name + "        id=" + id);
  6.         }

  7.         public String subName(String name) {
  8.                 return name;
  9.         }

  10.         @SuppressWarnings("unused")
  11.         private void addFri(int id) {
  12.                 System.out.println("添加" + id + "为朋友");
  13.         }

  14.         public Person() {
  15.         };

  16.         public Person(int id, String name) {
  17.                 this.id = id;
  18.                 this.name = name;
  19.         }

  20.         public int getId() {
  21.                 return id;
  22.         }

  23.         public void setId(int id) {
  24.                 this.id = id;
  25.         }

  26. }
  27. public class Fanshe_zonghe {

  28.         public Fanshe_zonghe() {
  29.                 // TODO Auto-generated constructor stub
  30.         }

  31.         /**
  32.          * @param args
  33.          */
  34.         public static void main(String[] args) {
  35.                 try {
  36.                         Class<?> clazz = Class.forName("com.drz.fanshe.Person");
  37.                         Field[] fields = clazz.getFields();
  38.                         // Object obj=clazz.newInstance();
  39.                         Constructor<?> constructor = clazz.getConstructor(int.class, java.lang.String.class);
  40.                         Object obj = constructor.newInstance(new Integer(234), "lihua");
  41.                         // Object obj = clazz.newInstance();
  42.                         for (Field field : fields) {
  43.                                 Object o = field.get(obj);
  44.                                 System.out.println(o);
  45.                         }
  46.                         Field fieldPri = clazz.getDeclaredField("id");
  47.                         fieldPri.setAccessible(true);
  48.                         Object oPri = fieldPri.get(obj);
  49.                         System.out.println(oPri);
  50.                         //
  51.                         Method methodP=clazz.getMethod("printInfo", new Class[]{});
  52.                         //methodP.invoke(obj, null); 这样会出现警告
  53.                         methodP.invoke(obj, new Object[]{});
  54.                 } catch (Exception e) {
  55.                         // TODO Auto-generated catch block
  56.                         e.printStackTrace();
  57.                 }

  58.         }

  59. }
复制代码



用反射拿到成员方法和使用成员方法时,参数列表为null怎么会报这个警告:The argument of type null should explicitly be cast to Object[] for the invocation of the varargs method invoke(Object, Object...) from type Method. It could alternatively be cast to Object for a varargs invocation

4 个回复

倒序浏览
lihua
234
name=lihua        id=234
------------------------------------
一点没改,完全粘贴复制的你的代码。。。。
回复 使用道具 举报
aurora_bessie 发表于 2015-7-22 09:28
lihua
234
name=lihua        id=234

我把会出现警告的语句注释掉了,代码里运行的是不出现警告的
//methodP.invoke(obj, null); 这样会出现警告
                        methodP.invoke(obj, new Object[]{});
回复 使用道具 举报
我看走眼了,,你定义的那个printInfo() 函数是无参的啊
Method methodP=clazz.getMethod("printInfo", null);
methodP.invoke(obj, null);
这样就不会出现警告了
你的错误可以这样理解,已经声明了有printInfo() 这个方法形参类型的Class对象,但你又设为空,矛盾啊
或者printInfo() 你定义的那个方法本身就是空参的,但你声明有参?
-----------------------------------------------前后对应就是啦,我也初学理解纯粹蒙希望有点帮助
回复 使用道具 举报
aurora_bessie 发表于 2015-7-22 10:09
我看走眼了,,你定义的那个printInfo() 函数是无参的啊
Method methodP=clazz.getMethod("printInfo", nul ...

对于代码里的警告也是不能忍
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马