3、得到某个对象的属性 a、非静态属性: 首先得到class,然后得到这个class具有的field,然后以具体实例为参数调用这个field
public Object getProperty(Object owner, String fieldName) throws Exception { Class ownerClass = owner.getClass();//首先得到class Field field = ownerClass.getField(fieldName); //然后得到这个class具有的field,也可以通过getFields()得到所有的field Object property = field.get(owner); //owner指出了取得那个实例的这个属性值,如果这个属性是非公有的,这里会报IllegalAccessException return property; } b、静态属性: 只有最后一步不同,由于静态属性属于这个类,所以只要在这个类上调用这个field即可 Object property = field.get(ownerClass); 4、执行某对象的方法 public Object invokeMethod(Object owner, String methodName, Object[] args) throws Exception { Class ownerClass = owner.getClass(); //也是从class开始的 //得到参数的class数组,相当于得到参数列表的类型数组,来取决我们选择哪个函数。 Class[] argsClass = new Class[args.length]; for (int i = 0, j = args.length; i < j; i++) { argsClass[i] = args[i].getClass(); } //根据函数名和函数类型来选择函数 Method method = ownerClass.getMethod(methodName, argsClass); return method.invoke(owner, args);//具体实例下,具体参数值下调用此函数 } 5、执行类的静态方法 和上面的相似只是最后一行不需要指定具体实例 return method.invoke(null, args); 6、判断是否为某个类的实例 public boolean isInstance(Object obj, Class cls) { return cls.isInstance(obj); } 测试bean类:SimpleBean.java Java代码 package com.royzhou.bean; public class SimpleBean { private String name; private String[] hobby; public SimpleBean() { } public SimpleBean(String name, String[] hobby) { this.name = name; this.hobby = hobby; } public void setName(String name) { this.name = name; } public String getName() { return this.name; } public void setHobby(String[] hobby) { this.hobby = hobby; } public String[] getHobby() { return this.hobby; } public String toString() { String returnValue = super.toString() + "\n"; returnValue += "name:=" + this.name + "\n"; if(this.hobby != null) { returnValue += "hobby:"; for(String s : this.hobby) { returnValue += s + ","; } returnValue += "\n"; } return returnValue; } } 反射测试类:ReflectTest.java Java代码 package com.royzhou.bean; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; public class ReflectTest { public static void main(String[] args) throws Exception { Class clazz = SimpleBean.class; //使用无参构造函数实例化bean SimpleBean sb = (SimpleBean)clazz.newInstance(); System.out.println(sb); //使用有参构造函数实例化bean Constructor constructor = clazz.getConstructor(new Class[]{String.class, String[].class}); sb = (SimpleBean)constructor.newInstance(new Object[]{"royzhou",new String[]{"football","basketball"}}); System.out.println(sb); //为name字段设置值 Field field = clazz.getDeclaredField("name"); field.setAccessible(true); //避免private不可访问抛出异常 field.set(sb, "royzhou1985"); System.out.println("modify name using Field:=" + sb.getName() + "\n"); //列出类SimpleBean的所有方法 Method[] methods = clazz.getDeclaredMethods(); System.out.println("get methods of class SimpleBean:"); for(Method method : methods) { if("setHobby".equals(method.getName())) { //动态调用类的方法来为hobby设置值 method.invoke(sb, new Object[]{new String[]{"tennis","fishing"}}); } System.out.println(method.getName()); } System.out.println("\nset by invoke Method"); System.out.println(sb); } } 输出结果: com.royzhou.bean.SimpleBean@757aef name:=null com.royzhou.bean.SimpleBean@d9f9c3 name:=royzhou hobby:football,basketball, modify name using Field:=royzhou1985 get methods of class SimpleBean: setHobby getHobby getName toString setName set by invoke Method com.royzhou.bean.SimpleBean@d9f9c3 name:=royzhou1985 hobby:tennis,fishing,