java反射

  反射:就是加载类,然后在解剖类的各个组成部分(反射都是用于做框架的)

  1 /**
  2      * 反射--->提取构造方法-->public 
  3      * @throws Exception
  4      */
  5     public void test() throws Exception{
  6         Class clazz = Class.forName("com.tkbs.du.refle.Person");
  7         Constructor c = clazz.getConstructor(null);
  8         Person p = (Person) c.newInstance(null);
  9         System.out.println(p.context);
 10         System.out.println("*************************************");
 11         Constructor constructor = clazz.getConstructor(String.class,String.class,int.class);
 12         Person person = (Person) constructor.newInstance("aaa","123",12);
 13         System.out.println(person.getAge());
 14         System.out.println(person.getName());
 15         
 16     }
 17     /**
 18      * 反射--->提取构造方法-->private
 19      * @throws Exception
 20      */
 21     public void test1() throws Exception{
 22         Class clazz = Class.forName("com.tkbs.du.refle.Person");
 23         Constructor c = clazz.getDeclaredConstructor(String.class);
 24         c.setAccessible(true);//暴力反射
 25         Person p = (Person) c.newInstance("du");
 26     }
 27     
 28     /**
 29      * 反射--->直接new方法
 30      * @throws Exception
 31      */
 32     public void test2() throws Exception{
 33         Class clazz = Class.forName("com.tkbs.du.refle.Person");
 34         Person p = (Person) clazz.newInstance();
 35         System.out.println(p.context);
 36     }
 37     /**
 38      * 正常方法反射调用
 39      * @throws Exception
 40      */
 41     public void test3() throws Exception{
 42         Person p = new Person();
 43         Class clazz = Class.forName("com.tkbs.du.refle.Person");
 44         Method methof = clazz.getMethod("aa", null);
 45         methof.invoke(p, null);
 46     }
 47     /**
 48      * 私有方法反射调用
 49      * @throws Exception
 50      */
 51     public void test4() throws Exception{
 52         Person p = new Person();
 53         Class clazz = Class.forName("com.tkbs.du.refle.Person");
 54         Method methof = clazz.getDeclaredMethod("find", null);
 55         methof.setAccessible(true);
 56         methof.invoke(p, null);
 57     }
 58     /**
 59      * 有参方法--反射调用
 60      * @throws Exception
 61      */
 62     public void test5() throws Exception{
 63         Person p = new Person();
 64         Class clazz = Class.forName("com.tkbs.du.refle.Person");
 65         Method method = clazz.getMethod("aa", String.class,int.class);
 66         method.invoke(p, "老大",12);
 67         
 68     }
 69     /**
 70      * 静态方法调用,带返回值
 71      * @throws Exception
 72      */
 73     public void test6() throws Exception{
 74         Class clazz = Class.forName("com.tkbs.du.refle.Person");
 75         Method method = clazz.getMethod("bb", String[].class);
 76         String[] str = (String[]) method.invoke(null, new Object[]{new String[]{"1","2","3","4"}});
 77         System.out.println(str);
 78     }
 79     /**
 80      * 调用mian方法
 81      * @throws Exception
 82      */
 83     public void test7() throws Exception{
 84         Class clazz = Class.forName("com.tkbs.du.refle.Person");
 85         Method method = clazz.getMethod("main", String[].class);
 86         method.invoke(null, (Object)new String[]{"aa","bb"});
 87         
 88     }
 89     /**
 90      * 反射出字段 public
 91      * @throws Exception
 92      */
 93     public void test8() throws Exception{
 94         Person p = new Person();
 95         Class clazz = Class.forName("com.tkbs.du.refle.Person");
 96         Field filed = clazz.getField("context");
 97         filed.set(p, "456");
 98         System.out.println(filed.get(p));
 99         
100         
101     }
102     
103     /**
104      * 反射出字段 private
105      * @throws Exception
106      */
107     @Test
108     public void test9() throws Exception{
109         Person p = new Person();
110         Class clazz = Class.forName("com.tkbs.du.refle.Person");
111         Field filed = clazz.getDeclaredField("context1");
112         filed.setAccessible(true);
113         System.out.println(filed.get(p));
114     }
115     
116     /**************************反射****************************************/

  在反射mian方法时有错,jdk1.4升级到jdk5.0出现了,mian方法的参数如果是这样写method.invoke(null,new String[]{"aa","bb"});它会抛一个异常,jvm会把aa和bb解析成两个参数,所以控制台会抛出一个参数错误的异常。

  解决办法:method.invoke(null,new Object[]{new String[]{"aa","bb"}});  或者 invoke(null,(Object)new String[]{"aa","bb"};

posted @ 2014-01-08 14:10  哎呦喂,我的小祖宗╰つ  阅读(244)  评论(0编辑  收藏  举报