反射机制 反射的应用 ---取得类的结构

1  取得所实现的全部接口

 1 public class InstanceDemo {
 2     public static void main(String[] args) {
 3         Class<?> c = null;
 4         try {
 5             c = Class.forName("com.matto.InstanceDemo.Person");
 6         } catch (ClassNotFoundException e) {
 7             e.printStackTrace();
 8         }
 9         Class<?>[] c1 = c.getInterfaces();
10         for( int i=0 ; i<c1.length ; i++ ){
11             System.out.println(c1[i].getName());
12         }
13     }
14 }

2  取得父类

 1 public class InstanceDemo {
 2     public static void main(String[] args) {
 3         Class<?> c = null;
 4         try {
 5             c = Class.forName("com.matto.InstanceDemo.Person");
 6         } catch (ClassNotFoundException e) {
 7             e.printStackTrace();
 8         }
 9         
10         Class<?> c1 = c.getSuperclass();
11         System.out.println(c1.getName());
12     }
13 }

3  取得全部构造方法

 1 public class InstanceDemo {
 2     public static void main(String[] args) {
 3         Class<?> c = null;
 4         try {
 5             c = Class.forName("com.matto.InstanceDemo.Person");
 6         } catch (ClassNotFoundException e) {
 7             e.printStackTrace();
 8         }
 9 
10         Constructor<?>[] cons = c.getConstructors();
11         
12         for( int i=0 ; i<cons.length ; i++ ){
13             System.out.println(cons[i].getName());
14         }
15     }
16 

4  取得全部方法

 1 public class InstanceDemo {
 2     public static void main(String[] args) {
 3         Class<?> c = null;
 4         try {
 5             c = Class.forName("com.matto.InstanceDemo.Person");
 6         } catch (ClassNotFoundException e) {
 7             e.printStackTrace();
 8         }
 9 
10         Method[] method = c.getMethods();
11         
12         for( int i=0 ; i<method.length ; i++ ){
13             System.out.println(method[i].getName());          //取得方法名
14             System.out.println(method[i].getReturnType());       //取得返回值类型
15             System.out.println(method[i].getExceptionTypes());    //取得异常抛出
16         }
17     }
18 }

5  取得全部属性

 1 public class InstanceDemo {
 2     public static void main(String[] args) {
 3         Class<?> c = null;
 4         try {
 5             c = Class.forName("com.matto.InstanceDemo.Person");
 6         } catch (ClassNotFoundException e) {
 7             e.printStackTrace();
 8         }
 9         System.out.println("----------------------输出本类属性----------------------");
10         Field[] f = c.getDeclaredFields();          //获取属性
11 
12         for( int i=0 ; i<f.length ; i++ ){
13             Class<?> r = f[i].getType();            //获取属性的类型
14             int mo = f[i].getModifiers();           //获取修饰符数字
15             String priv = Modifier.toString(mo);    //根据修饰符数字取得修饰符
16             System.out.println(priv + " ");         //输出修饰符
17             System.out.println(r.getName() + " ");  //输出属性类型
18             System.out.println(f[i].getName() + ";");     //输出属性名称
19         }
20         System.out.println("----------输出公共属性即实现的接口或父类中的公共属性----");
21         Field[] f = c.getFields();          //获取属性
22 
23         for( int i=0 ; i<f.length ; i++ ){
24             Class<?> r = f[i].getType();            //获取属性的类型
25             int mo = f[i].getModifiers();           //获取修饰符数字
26             String priv = Modifier.toString(mo);    //根据修饰符数字取得修饰符
27             System.out.println(priv + " ");         //输出修饰符
28             System.out.println(r.getName() + " ");  //输出属性类型
29             System.out.println(f[i].getName() + ";");     //输出属性名称
30         }
31     }
32 }

 IDE的输入.就可以获得类的方法或属性就是基于反射的原理

posted @ 2016-06-15 12:01  Blog4Matto  阅读(211)  评论(0编辑  收藏  举报