码迷,mamicode.com
首页 > 编程语言 > 详细

java反射详解及说明

时间:2016-05-05 00:31:36      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

首先写一个Person类:

 1 package lltse.base.reflectdemo;
 2 
 3 public class Person 
 4 {
 5     
 6     private String name;
 7     
 8     public String getName() {
 9         return name;
10     }
11 
12 
13 
14     public void setName(String name) {
15         this.name = name;
16     }
17 
18 
19 
20     public int getSex() {
21         return sex;
22     }
23 
24 
25 
26     public void setSex(int sex) {
27         this.sex = sex;
28     }
29 
30 
31 
32     public int getAge() {
33         return age;
34     }
35 
36 
37 
38     public void setAge(int age) {
39         this.age = age;
40     }
41 
42 
43 
44     private int sex;
45     
46     private int age;
47     
48     public Person(String name,int sex,int age)
49     {
50         this.name = name;
51         this.sex = sex;
52         this.age = age;
53     }
54     
55     public Person()
56     {
57     }
58 
59     /**
60      * @param args
61      */
62     public static void main(String[] args) {
63         // TODO Auto-generated method stub
64 
65     }
66     
67     
68     public int show(String name,int age)
69     {
70         System.out.println("showInfo:>>>:name"+name+"age:>>>"+age);
71         return age;
72     }
73 
74     public static String getName(String name)
75     {
76         return name;
77     }
78 }

其次是测试反射的相关方法

 1 package lltse.base.reflectdemo;
 2 
 3 import java.lang.reflect.Constructor;
 4 import java.lang.reflect.InvocationTargetException;
 5 import java.lang.reflect.Method;
 6 
 7 public class ReflectTest 
 8 {
 9     /**
10      * @param args
11      * @throws ClassNotFoundException 
12      * @throws SecurityException 
13      * @throws NoSuchMethodException 
14      * @throws InvocationTargetException 
15      * @throws IllegalArgumentException 
16      * @throws IllegalAccessException 
17      * @throws InstantiationException 
18      */
19     public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException 
20     {
21         //获取Class方法一
22         //Person person = new Person();
23         //Class personClazz1 = person.getClass();
24         
25         //获取Class方法二
26         Class  personClazz2 = Person.class;
27         
28         //获取Class方法三
29         Class personClazz3 = Class.forName("lltse.base.reflectdemo.Person");
30         
31         //活动完整的包名和类名
32         System.out.println("personClazz3.getName():>>>"+personClazz3.getName());
33         
34         /* 返回一个包含某些 Method 对象的数组,这些对象反映此 Class 
35         对象所表示的类或接口(包括那些由该类或接口声明的以及从超类和超接口继承的那些的类或接口)的公共 member 方法。*/
36         Method[] methods = personClazz3.getMethods();
37         
38          /*返回 Method 对象的一个数组,这些对象反映此 Class 对象表示的类或接口声明的所有方法,
39          包括公共、保护、默认(包)访问和私有方法,但不包括继承的方法。*/
40         Method[] declaredMethods = personClazz3.getDeclaredMethods();
41         
42         
43         for(int i=0;i<methods.length;i++)
44         {
45             System.out.println(methods[i].getName());
46         }
47         
48         //返回一个包含某些 Constructor 对象的数组,这些对象反映此 Class 对象所表示的类的所有公共构造方法。
49         Constructor[] constructors = personClazz3.getConstructors();
50         
51         for(int t=0;t<constructors.length;t++)
52         {
53             System.out.println("constructors["+t+"]:>>>>"+constructors[t]);
54         }
55 
56         //实例化类对象
57         Person p = (Person)personClazz3.newInstance();
58         //返回一个 Method 对象,它反映此 Class 对象所表示的类或接口的指定公共成员方法。
59         Method method = personClazz3.getMethod("show", new Class[]{String.class,int.class});
60         
61         //对带有指定参数的指定对象调用由此 Method 对象表示的底层方法。p代表被实例化的类对象
62         int returnValue = (int)method.invoke(p, "zhangsan",30);
63         System.out.println("returnValue:>>>"+returnValue);
64         
65         /*如果底层方法是静态的,那么可以忽略指定的 obj 参数。该参数可以为 null。 
66         如果底层方法所需的形参数为 0,则所提供的 args 数组长度可以为 0 或 null。*/
67 
68         Method method2 = personClazz3.getMethod("getName", new Class[]{String.class});
69         String name = (String)method2.invoke(null, "test");
70         System.out.println("name:>>>"+name); 
71         
72         
73     }
74     
75     
76 
77 }

 

java反射详解及说明

标签:

原文地址:http://www.cnblogs.com/lltse/p/5460280.html

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