码迷,mamicode.com
首页 > 其他好文 > 详细

反射精讲(框架程序员的必经之路)

时间:2017-09-25 23:52:13      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:lan   cto   理解   输出   row   技术分享   bsp   color   int   

反射的概念:

 小博的个人理解就是在你不知道某个类的构造方法、成员变量、成员方法时候,(嗯哼~),你就可以采取反射的方式来对它进行自己的操作。

 Student类:

技术分享
 1 package Reflect;
 2 
 3 import java.util.Date;
 4 
 5 public class Student {
 6     private String name;
 7     private int age;
 8     private Date date;
 9     
10     public Student(){
11         System.out.println("This is a null 构造");
12     }
13     public Student(String name){
14         this.name =name;
15     }
16     //私有的构造
17     private Student(Date date){
18         this.date =date;
19     }
20     public Student(String name,int age){
21         this.name =name;
22         this.age =age;
23     }
24     public String getName() {
25         return name;
26     }
27     public void setName(String name) {
28         this.name = name;
29     }
30     public int getAge() {
31         return age;
32     }
33     public void setAge(int age) {
34         this.age = age;
35     }
36     public Date getData() {
37         return date;
38     }
39     public void setData(Date date) {
40         this.date = date;
41     }
42     
43 }
View Code  

 针对于构造方法:

技术分享
 1 package Reflect;
 2 
 3 import java.lang.reflect.Constructor;
 4 import java.util.Date;
 5 
 6 import org.junit.Test;
 7 
 8 public class ReflectDemo {
 9     //获取构造方法 一共三种方式(框架程序员使用)
10     /*@Test
11     public void test1() throws Exception, Exception{
12         Class clazz  =  Student.class;
13         Constructor c = clazz.getConstructor(null);
14         Student s = (Student)c.newInstance(null);
15         System.out.println(s);
16         
17     }*/
18     /*@Test
19     public void test1() throws Exception, Exception{
20         Class clazz  =  Student.class;
21         Object object = clazz.newInstance();
22         System.out.println(object.getClass().getName());
23         
24     }*/
25     @Test
26     public void test1() throws Exception, Exception{
27         Class c =Class.forName("Reflect.Student");
28         Student s =(Student) c.newInstance();
29         System.out.println(s);
30         
31     }
32     //获取有参数构造
33     @Test
34     public void test2() throws Exception, Exception{
35     Class clazz =Student.class;
36     //获取构造函数
37     Constructor c = clazz.getConstructor(String.class);
38     Student s =(Student)c.newInstance("赵日天");
39     System.out.println(s.getName());
40     }
41     //获取私有的构造方法
42     @Test
43     public void test3() throws Exception, Exception{
44         Class clazz =Student.class;
45         Constructor an = clazz.getDeclaredConstructor(Date.class);
46         //暴力反射
47         an.setAccessible(true);
48         Student s =(Student)an.newInstance(new Date());
49         System.out.println(s.getData());
50                 
51     }
52 }
RefelctDemo

  字段和成员方法:

技术分享
 1 package Reflect;
 2 
 3 public class Person {
 4     //普通成员变量
 5     public String name;
 6     //私有的
 7     private int age;
 8     //静态的
 9     public static String address;
10     
11     public int getAge() {
12         return age;
13     }
14     public void setAge(int age) {
15         this.age = age;
16     }
17     
18     //创建一个普通的成员方法
19     public void m1(){
20         System.out.println("无参数无返回值的方法");
21     }
22     //创建一个有参数的成员方法
23     public void m2(String name){
24         System.out.println("有参数"+name);
25     }
26     //创建一个静态的方法
27     public static void m3(String name){
28         System.out.println("静态");
29     }
30     //私有的
31     private static void m4(){
32         System.out.println("私有");
33     }
34 }
Person
技术分享
  1 package Reflect;
  2 
  3 import java.lang.reflect.Field;
  4 import java.lang.reflect.Method;
  5 
  6 import org.junit.Test;
  7 
  8 public class Reflect02Demo {
  9     //反射测试字段
 10     @Test
 11     public void test05() throws Exception, Exception{
 12         //获取字节码文件
 13         Class clazz = Person.class;
 14         //获取字段函数
 15         Field field = clazz.getField("name");
 16         Person p  =(Person)clazz.newInstance();
 17         //读取字段值
 18         String s =(String)field.get(p);
 19         //输出
 20         System.out.println(s);
 21         //设置值
 22         field.set(p,"杨先生");
 23         System.out.println(p.name);
 24     }
 25     //私有的字段
 26     @Test
 27     public void test06() throws Exception, Exception{
 28         //获取字节码文件
 29         Class clazz = Person.class;
 30         //获取字段函数
 31         Field field = clazz.getDeclaredField("age");
 32         //暴力反射
 33         field.setAccessible(true);
 34         Person p  =(Person)clazz.newInstance();
 35         //读取字段值
 36         int i =(Integer)field.get(p);
 37         //输出
 38         System.out.println(i);
 39         //设置值
 40         field.set(p,22);
 41         System.out.println(p.getAge());
 42     }
 43     //静态字段
 44     @Test
 45     public void test07() throws Exception, Exception{
 46         //获取字节码文件
 47         Class clazz = Person.class;
 48         //获取字段函数
 49         Field field = clazz.getField("address");
 50         //读取字段值
 51         String s =(String)field.get(null);
 52         //输出
 53         System.out.println(s);
 54         //设置值
 55         field.set(null,"山东");
 56         System.out.println(Person.address);
 57     }
 58     
 59     
 60     //测试反射获取普通的方法
 61     @Test
 62     public void test01() throws Exception, Exception{
 63         //获取字节码文件
 64         Class clazz = Person.class;
 65         //获取构造函数,null表示该参数用到的类型为空
 66         Method method = clazz.getDeclaredMethod("m1", null);
 67         //调用方法,null表示无参数
 68         method.invoke(clazz.newInstance(), null);
 69     }
 70     //测试反射获取有参数方法
 71     @Test
 72     public void test02() throws Exception, Exception{
 73         //获取字节码文件
 74         Class clazz = Person.class;
 75         //获取构造函数,null表示该参数用到的类型为空
 76         Method method = clazz.getDeclaredMethod("m2", String.class);
 77         //调用方法,null表示无参数
 78         method.invoke(clazz.newInstance(), "黎明");
 79     }
 80     //测试反射获取静态方法
 81         @Test
 82         public void test03() throws Exception, Exception{
 83             //获取字节码文件
 84             Class clazz = Person.class;
 85             //获取构造函数,null表示该参数用到的类型为空
 86             Method method = clazz.getDeclaredMethod("m3", String.class);
 87             //调用方法,null表示无参数
 88             method.invoke(null,"大白");
 89         }
 90         //测试反射获取静态私有方法
 91         @Test
 92         public void test04() throws Exception, Exception{
 93             //获取字节码文件
 94             Class clazz = Person.class;
 95             //获取构造函数,null表示该参数用到的类型为空
 96             Method method = clazz.getDeclaredMethod("m4",null);
 97             //暴力反射
 98             method.setAccessible(true);
 99             //调用方法,null表示无参数
100             method.invoke(null,null);
101         }
102         
103 }
Reflect02Demo

      

        额外的注意事项,关于参数是数组类型:

 

技术分享

 

  

反射精讲(框架程序员的必经之路)

标签:lan   cto   理解   输出   row   技术分享   bsp   color   int   

原文地址:http://www.cnblogs.com/zuzizai/p/7594368.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!