java反射简单实例
这篇博友的总结的反射知识点是比较全面的
http://www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.html
下面介绍我用反射做的两个功能
1、列出被反射类所有的属性和继承自接口或者父类的属性
2、调用反射类的方法
3、通过反射调用被反射类的setter方法
先列出接口类,父类,被反射的类
接口类:
package com.test.reflect.helper; public interface ChinaInterface { public static final String name="Rollen"; public static int age=20; public void sayChina(); public void sayHello(String name, int age); }
父类
package com.test.reflect.helper; public class FatcherClass { public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } private String birthday; //private不会继承 public String sex; //public 的属性会被继承 }
反射类
package com.test.reflect.helper; public class Person extends FatcherClass implements ChinaInterface{ public Person() { } public Person(String name) { this.name = name; } public Person(int age) { this.age = age; } public Person(String name, int age) { this.age = age; this.name = name; } public void setName(String name) { this.name=name; } public void setAge(int age) { this.age=age; } public String getName() { return name; } public int getAge() { return age; } @Override public String toString() { return "[" + this.name + " " + this.age + "]"; } private String name; private int age; @Override public void sayChina() { // TODO Auto-generated method stub System.out.println(" China, hi nice to meet you!"); } @Override public void sayHello(String name, int age) { // TODO Auto-generated method stub System.out.println("hello ,i am "+name+"and i am "+age +"yeas old"); } }
下面我们实现功能一:列出被反射类所有的属性和继承自接口或者父类的属性
package com.test.reflect; import java.lang.reflect.Field; import java.lang.reflect.Modifier; public class GetAllFieldsAndInterfaceParentClass { public static void main(String[] args) { method(); } public static void method() { Class<?> demo = null; try { // 取得object的 实例,不是对象 demo = Class.forName("com.test.reflect.Person"); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } System.out.println("-------------获取object所有的属性--------------"); // 获取 所有的声明的字段 Field[] fieldArray = demo.getDeclaredFields(); for (int i = 0; i < fieldArray.length; i++) { // 获取权限修饰符 返回int类型,再转化为String类型 int mo = fieldArray[i].getModifiers(); String priv = Modifier.toString(mo); // 获取字段的类型 Class<?> type = fieldArray[i].getType(); System.out.println(priv + " " + type.getName() + " " + fieldArray[i].getName()); } System.out.println("-------------实现的接口或者父类的属性--------------"); //获取所有父类或继承的接口 Field[] inhertArray=demo.getFields(); System.out.println(inhertArray.length); for(int j=0;j<inhertArray.length;j++) { //获取权限 int mo=inhertArray[j].getModifiers(); String priv=Modifier.toString(mo); //获取接口或类的类型 Class<?> type=inhertArray[j].getType(); System.out.println(priv+" "+type.getName()+" "+inhertArray[j].getName()); } } }
结果:
-------------获取object所有的属性--------------
private java.lang.String name
private int age
-------------实现的接口或者父类的属性--------------
3
public static final java.lang.String name
public static final int age
public java.lang.String sex
实现功能二、调用反射类的方法
package com.test.reflect; import java.lang.reflect.Method; public class UseReflectMethod { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("ok"); Class<?> demo = null; try { demo = Class.forName("com.test.reflect.helper.Person"); } catch (ClassNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { //获取被反射类的方法 Method method = demo.getMethod("sayChina"); //传递类实例,调用对应方法 method.invoke(demo.newInstance()); //获取被反射类的方法,并说明所要传递的参数的类型 Method method2=demo.getMethod("sayHello",String.class,int.class); //传递参数,并调用方法 method2.invoke(demo.newInstance(), "jack",234); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
结果:
China, hi nice to meet you!
hello ,i am jackand i am 234yeas old
实现功能三:通过反射调用被反射类的setter方法
package com.test.reflect; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; public class UseGetterAndSetter { public static void main(String [] args) { Class<?> demo=null; try { demo=Class.forName("com.test.reflect.helper.Person"); Object obj=demo.newInstance(); Field[] declaredFieldArray=demo.getDeclaredFields(); for(int i=0;i<declaredFieldArray.length;i++) { //获取修饰符,访问 权限 int mo=declaredFieldArray[i].getModifiers(); String priv=Modifier.toString(mo); //获取变量类型 Class<?> type=declaredFieldArray[i].getType(); if(type.getName()=="java.lang.String") invokeGetAndSet(type,declaredFieldArray[i].getName(),obj,"jackvin"); else if(type.getName()=="int") invokeGetAndSet(type,declaredFieldArray[i].getName(),obj,23); } /*反射调用setter和getter的一般方法 * Object obj=demo.newInstance(); *Method methodSetName=demo.getMethod("setName",String.class); *methodSetName.invoke(obj, "jackvin"); *Method methodSetAge=demo.getMethod("setAge", int.class); *methodSetAge.invoke(obj, 12); *System.out.println(obj.toString()); */ System.out.println(obj); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } public static void invokeGetAndSet(Class<?> type,String field,Object obj,String fieldValue) { //拼接成setFiled()方法的名字 String methodName="set"+field.substring(0,1).toUpperCase()+field.substring(1); try { Method method= obj.getClass().getMethod(methodName, type); method.invoke(obj,fieldValue); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void invokeGetAndSet(Class<?> type,String field,Object obj,int fieldValue) { //拼接成setFiled()方法的名字 String methodName="set"+field.substring(0,1).toUpperCase()+field.substring(1); try { Method method= obj.getClass().getMethod(methodName, type); method.invoke(obj,fieldValue); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
结果:
[jackvin 23]