一:
package Test2;
import Test.Person;import org.junit.Test;import java.lang.reflect.Field;
import java.lang.reflect.Modifier;/*
获取当前运行时类的所有属性*/
public class FieldTest {@Testpublic void test1(){Class clazz = Person.class;//获取属性结构Field[] fields = clazz.getFields();for(Field f :fields){//只能获取当前类及其父类权限为public的属性System.out.println(f);}System.out.println("");//getDeclaredFields() :获取当前类的虽有属性,包括私有(不包含父类)Field[] declaredFields = clazz.getDeclaredFields();for(Field f:declaredFields){System.out.println(f);}}//权限修饰符 数据类型 变量名@Testpublic void test2(){Class clazz = Person.class;Field[] declaredFields = clazz.getDeclaredFields();for(Field f:declaredFields){//1.权限修饰符//此时返回的为0:default,1:public,2:private 4:protectedint modifiers = f.getModifiers();//翻译过来 Modifier.toString()System.out.print(Modifier.toString(modifiers));//2.数据类型Class<?> type = f.getType();System.out.print(" "+type+"\t");//3.变量名String name = f.getName();System.out.println(name);}}
}
二:
package Test2;
import Test.Person;
import org.junit.Test;import java.lang.reflect.Method;/*
获取运行时类的方法结构*/
public class MethodTest {@Testpublic void test(){Class clazz = Person.class;//getMethods() :获取当前运行时类及其父类所有的声明为public权限的方法Method[] method = clazz.getMethods();for(Method m : method){System.out.println(m );}System.out.println();//getDeclaredMethods():获取当前运行时类的所有方法(不包含父类)Method[] declaredMethods = clazz.getDeclaredMethods();for(Method m :declaredMethods){System.out.println(m);}}@Testpublic void test2(){}
}
(重!)三:
package Test2;
import Test.Person;import org.junit.Test;import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;/*
调用运行时类中指定的结构:属性、方法、构造器*/
public class ReflectionTest {/*get、set*/@Testpublic void testField() throws Exception {Class clazz = Person.class;//创建运行时类的对象Person person =(Person) clazz.newInstance();//获取指定属性Field id = clazz.getField("id");/*设置当前属性的值:要求运行时类中的属性为public通常不采用此方法id.set(person,1001);set(): 参数1:指明设置哪个对象的属性 参数2:value*///获取运行时类的指定变量名的属性Field name = clazz.getDeclaredField("name");//在设置前需要获取对Private 和 defualt 权限属性的 权限name.setAccessible(true);name.set(person,"tom");System.out.println(name.get(person ));/*获取当前属性的值get(): 参数1:获取哪个对象的属性*/Object o = id.get(person);System.out.println(o);}/*如何操作运行时类指定的方法*/@Testpublic void testMethod() throws Exception{Class clazz = Person.class;Person p = (Person)clazz.newInstance();//1.获取某个指定的方法 参数1:指明获取方法的名称 参数2:因为可能重载的原因,指明获取方法的参数列表Method show = clazz.getDeclaredMethod("show", String.class);//保证当前方法是可访问的show.setAccessible(true);// invoke(): 参数1:方法的调用者 参数2:给方法的参数赋值// invoke()方法的返回值即为对应类中调用的方法的返回值Object china = show.invoke(p, "China");System.out.println(china);//调用静态方法Method showDesc = clazz.getDeclaredMethod("showDesc");showDesc.setAccessible(true);//若调用运行时类的方法没有返回值,在返回null//因为静态方法,每个对象都是一样的,所以不需要指定调用对象,但不能为空 Object.class , null, Person.class....Object invoke = showDesc.invoke(null);System.out.println(invoke);//null}/*如何调用运行时类中指定的构造器*/@Testpublic void testConstructor() throws Exception{Class clazz = Person.class;//1.获取指定构造器(构造器名称都一样) 参数:指明构造器的参数列表Constructor declaredConstructor = clazz.getDeclaredConstructor(String.class);//2.保证此构造器是可访问的declaredConstructor.setAccessible(true);//3.调用此构造器创建运行时类的对象Object o = declaredConstructor.newInstance("Tom");System.out.println(o);}}