Person: import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; public class ClassTest2 { public static void main(String[]args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{ //获取 Person类对象 Class c = Person.class; //新建一个Person对象,在获取对象属性值的时候需要作为参数传 Person person = new Person(); Method[]methods = c.getMethods(); for(Method m:methods){ if(m.getName().equals("setId")){ m.invoke(person, 1); } if(m.getName().equals("setName")){ m.invoke(person, "ningbingjian"); } } //获取类对象的属性域包括属性和属性值 Field[] fields = c.getDeclaredFields(); for(Field f:fields){ //针对不能访问的PRIVATE 属性,设置可访问性 f.setAccessible(true); System.out.println(Modifier.toString(f.getModifiers())+" "+f.getType()+" "+f.getName()+"值是:"+f.get(person)); } } } ClassTest: import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; public class ClassTest2 { public static void main(String[]args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{ //获取 Person类对象 Class c = Person.class; //新建一个Person对象,在获取对象属性值的时候需要作为参数传 Person person = new Person(); Method[]methods = c.getMethods(); for(Method m:methods){ if(m.getName().equals("setId")){ m.invoke(person, 1); } if(m.getName().equals("setName")){ m.invoke(person, "ningbingjian"); } } //获取类对象的属性域包括属性和属性值 Field[] fields = c.getDeclaredFields(); for(Field f:fields){ //针对不能访问的PRIVATE 属性,设置可访问性 f.setAccessible(true); System.out.println(Modifier.toString(f.getModifiers())+" "+f.getType()+" "+f.getName()+"值是:"+f.get(person)); } } }