Java反射机制

Java反射机制——获取成员变量&构造函数
一、成员变量是java.lang.reflect.Field的对象
1、Field类封装了关于成员变量的操作
2、Field[] fs = c.getFields()方法获取所有public的成员变量Field[]信息
3、c.getDeclaredFields获取的是该类自己声明的成员变量信息
4、field.getType()获得成员类型的类类型
5、field.getName()获得成员的名称
二、构造函数是java.lang.Constructor类的对象
1、通过Class.getConstructor()获得Constructor[]所有公有构造方法信息
2、建议getDeclaredConstructors()获取自己声明的构造方法
3、Constructor.getName():String
4、Constructor.getParameterTypes():Class[]
成员变量也是对象,是java.lang.reflect.Field的对象

 1 import java.lang.reflect.Constructor;
 2 import java.lang.reflect.Field;
 3 import java.lang.reflect.Method;
 4 
 5 public class ClassUtil {
 6     public static void printMethedsMessage(Object obj){
 7         Class cs=obj.getClass();
 8         Method []method =cs.getMethods();
 9         for(Method meth:method){
10             System.out.print(meth.getName()+"(");
11             Class ptype[] = meth.getParameterTypes();
12             for(Class type:ptype){
13                 System.out.print(type.getName()+",");
14             }
15             System.out.println(")");
16         }
17     }
18     public static void printConMessage(Object obj) {
19         Class c=obj.getClass();
20         Constructor[] con= c.getDeclaredConstructors();
21         for(Constructor constructor:con){
22             System.out.print(constructor+"(");
23             Class[]field =constructor.getParameterTypes();
24             for(Class ff:field){
25                 System.out.print(ff+",");
26             }
27             System.out.println(")");
28         }
29         
30     }
31 }

通过反射机制调用方法

 1 import java.lang.reflect.InvocationTargetException;
 2 import java.lang.reflect.Method;
 3 
 4 public class ClassMethod {
 5     public static void main(String arg[]){
 6         A a1= new A();
 7         Class cs=a1.getClass();//都要首先获取类类型
 8         try {
 9             Method m = cs.getMethod("print",new Class[]{});
10             try {
11                 /*
12                  * 对应的参数列表
13                  */
14                 m.invoke(new A());//利用反射机制反向调用方法
15             } catch (Exception e) {
16                 e.printStackTrace();
17             }
18         } catch (Exception e) {
19             e.printStackTrace();
20         } 
21     }
22 }
23 class A{
24     public void print(){
25         System.out.println("helloWorld");
26     }
27 }

通过反射了解泛型的本质

 1 import java.lang.reflect.Method;
 2 import java.util.ArrayList;
 3 
 4 public class fanxing {
 5 
 6     public static void main(String[] args) {
 7         ArrayList<String> list1 = new ArrayList<String>();
 8         ArrayList list = new ArrayList();
 9         list1.add("HelloWorld");
10         /*
11          * list1.add(20);//工具提示错误,说明eclipse是自动编译的
12          */
13         Class cs = list1.getClass();
14         Class no = list.getClass();
15         if(cs == no)
16             System.out.println(true);
17         /*
18          * 答案是true  说明编译之后的集合是去泛型化的
19          * Java集合的泛型,是防止输入错误的,只在编译时有效,绕过编译就无效了
20          * 我们可以用方法的反射来操作,绕过编译
21          */
22         try {
23             @SuppressWarnings("unchecked")
24             Method m = cs.getMethod("add",Object.class);
25             m.invoke(list1, 20);//绕过编译就绕过了泛型
26             System.out.println(list1);
27         } catch (Exception e) {
28             e.printStackTrace();
29         }
30         
31     }
32 
33 }

 

import java.lang.reflect.Method;import java.util.ArrayList;
public class fanxing {
public static void main(String[] args) {ArrayList<String> list1 = new ArrayList<String>();ArrayList list = new ArrayList();list1.add("HelloWorld");/* * list1.add(20);//工具提示错误,说明eclipse是自动编译的 */Class cs = list1.getClass();Class no = list.getClass();if(cs == no)System.out.println(true);/* * 答案是true  说明编译之后的集合是去泛型化的 * Java集合的泛型,是防止输入错误的,只在编译时有效,绕过编译就无效了 * 我们可以用方法的反射来操作,绕过编译 */try {@SuppressWarnings("unchecked")Method m = cs.getMethod("add",Object.class);m.invoke(list1, 20);//绕过编译就绕过了泛型System.out.println(list1);} catch (Exception e) {e.printStackTrace();}}
}

 

posted @ 2016-07-24 10:13  单链表  阅读(230)  评论(0编辑  收藏  举报