1、调用运行时类中的指定属性
1.1、Person类
package github2;@MyAnnotation(value="java")public class Person extends Creature<String> implements Comparable<String>,MyInterface{private String name;int age;public int id;public Person() {}@MyAnnotation(value="C++")Person(String name){this.name = name;}private Person(String name,int age){this.name = name;this.age = age;}@MyAnnotationprivate String show(String nation){System.out.println("我来自" + nation + "星系");return nation;}@Overridepublic void info() {System.out.println("火星喷子");}public String display(String interests,int age) throws Exception{return interests + age;}@Overridepublic int compareTo(String o) {return 0;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +", id=" + id +'}';}}
1.2、测试类
package github3;import github2.Person;import org.junit.Test;import java.lang.reflect.Field;/*** 调用运行时类中指定的结构:属性、方法、构造器*/public class ReflectionTest {/*** 不需要掌握*/@Testpublic void testField() throws Exception {Class clazz = Person.class;//创建运行时类的对象Person p = (Person) clazz.newInstance();//获取指定的属性:要求运行时类中属性声明为public//通常不采用此方法Field id = clazz.getField("id");//设置当前属性的值//set():参数1:指明设置哪个对象的属性 参数2:将此属性值设置为多少id.set(p,1001);//获取当前属性的值//get():参数1:获取哪个对象的当前属性值int pId = (int) id.get(p);System.out.println(pId);}/*** 如何操作运行时类中的指定的属性 -- 需要掌握*/@Testpublic void testField1() throws Exception {Class clazz = Person.class;//创建运行时类的对象Person p = (Person) clazz.newInstance();//1. getDeclaredField(String fieldName):获取运行时类中指定变量名的属性Field name = clazz.getDeclaredField("name");//2.保证当前属性是可访问的name.setAccessible(true);//3.获取、设置指定对象的此属性值name.set(p,"Jam");System.out.println(name.get(p));}}
2、调用运行时类中的指定方法
getDeclaredMethod():
参数1 :指明获取的方法的名称 参数2:指明获取的方法的形参列表
调用方法的invoke():
参数1:方法的调用者 参数2:给方法形参赋值的实参
invoke()的返回值即为对应类中调用的方法的返回值。
package github3;import github2.Person;import org.junit.Test;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;/*** 调用运行时类中指定的结构:属性、方法、构造器*/public class ReflectionTest {/*** 如何操作运行时类中的指定的方法 -- 需要掌握*/@Testpublic void testMethod() throws Exception {Class clazz = Person.class;//创建运行时类的对象Person p = (Person) clazz.newInstance();//1.获取指定的某个方法//getDeclaredMethod():参数1 :指明获取的方法的名称 参数2:指明获取的方法的形参列表Method show = clazz.getDeclaredMethod("show", String.class);//2.保证当前方法是可访问的show.setAccessible(true);//3.调用方法的invoke():参数1:方法的调用者 参数2:给方法形参赋值的实参//invoke()的返回值即为对应类中调用的方法的返回值。Object returnValue = show.invoke(p,"CCA"); //String nation = p.show("CCA");System.out.println(returnValue);System.out.println("+++++++++如何调用静态方法+++++++++++");// private static void showDesc()Method showDesc = clazz.getDeclaredMethod("showDown");showDesc.setAccessible(true);//如果调用的运行时类中的方法没有返回值,则此invoke()返回null// Object returnVal = showDesc.invoke(null);Object returnVal = showDesc.invoke(Person.class);System.out.println(returnVal);//null}}
3、调用运行时类中的指定构造器
import github2.Person;import org.junit.Test;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;/*** 调用运行时类中指定的结构:属性、方法、构造器*/public class ReflectionTest {/*** 如何调用运行时类中的指定的构造器*/@Testpublic void testConstructor() throws Exception {Class clazz = Person.class;//private Person(String name)//1.获取指定的构造器//getDeclaredConstructor():参数:指明构造器的参数列表Constructor constructor = clazz.getDeclaredConstructor(String.class);//2.保证此构造器是可访问的constructor.setAccessible(true);//3.调用此构造器创建运行时类的对象Person per = (Person) constructor.newInstance("Tom");System.out.println(per);}}
