反射的前提是获取当要反射类的Class对象,获取方式有如下几种:
1 //使用Class.forName 2 Class c = Class.forName("com.test.Person"); 3 //使用Class属性 4 Class c = Person.class; 5 //使用getClass 6 Person p = new Person(); 7 Class c = p.getClass(); 8 //获取基本类型的class可以使用如下方式 9 Class c = int.class;//输出 int 10 Class c = Integer.TYPE;//输出 int 11 Class c = Integer.class;//输出 java.lang.Integer
接着创建要反射的类Person
1 class Person { 2 private String name; 3 private int age; 4 //无参构造方法 5 public Person(){ 6 7 } 8 //有参构造函数 9 public Person(String name, int age) { 10 this.name = name; 11 this.age = age; 12 } 13 //输出信息 14 private void getInfo() { 15 System.out.println(name + "\t" + age); 16 } 17 //私有方法 18 private void privateMethod() { 19 System.out.println("调用了private方法"); 20 } 21 //私有静态方法 22 private static void getStaticPrivateMethod(){ 23 System.out.println("getStaticPrivateMethod"); 24 } 25 26 //public方法 27 public void getPublicMethod(){ 28 System.out.println("getPublicMethod"); 29 } 30 31 //public静态方法 32 public static void pubicStaticMethod() { 33 System.out.println("调用了public static方法"); 34 } 35 //带有参数的公共静态方法 36 public static void getMsg(String msg){ 37 System.out.println(msg); 38 } 39 40 }
1.反射私有方法
1 // 反射Person类中的private方法 2 Class clazz = Class.forName("com.test.Person"); 3 // 创建Person类的实例,前提是Person有一个无参构造方法 4 Person person = (Person) clazz.newInstance(); 5 // 获取Method,使用getDeclaredMethod是对私有方法进行访问,但是必须设置setAccessible为true 6 Method method = clazz.getDeclaredMethod("privateMethod"); 7 // 这是在访问私有方法时不对安全性进行检查 8 method.setAccessible(true); 9 // 调用person类中的getAge方法 10 method.invoke(person);
2.反射私有静态方法
//获取Person的Class对对象 Class c = Person.class; //获取要调用的方法,这里需要有getDeclareMethod来获取类的私有方法 Method m = c.getDeclaredMethod("getStaticPrivateMethod") //设置私有方法的可访问性为true m.setAccessible(true); //调用 m.invoke(c);
3.反射公共方法
//获取Class对象 Class c = Person.class; //构建Person对象(前提是Person有一个无参构造方法) Person person = (Person) c.newInstance(); //获取要调用的方法 Method method = c.getMethod("getPublicMethod"); //调用person对象中的方法 method.invoke(person);
4.反射公共静态方法
//获取Class对象 Class cc = Person.class; //获取要调用的方法 Method method = cc.getMethod("pubicStaticMethod"); //调用方法,因为是static方法,所以直接通过类来调用 method.invoke(cc);
5.反射带有参数的公共静态方法
//获取Class Class c = Person.class; //获取Method,要传递参数的Class对象数组,一个参数的情况也可以直接传class,如 String.class Method method = c.getMethod("getMsg",new Class[]{String.class}); method.invoke(c,"不能说的秘密");
6.反射有参构造函数的私有方法
//获取Class Class c = Person.class; //构建有参构造方法 Constructor constructor = c.getConstructor(new Class[]{String.class, int.class}); //通过constructor获取Person对象实例 Person person = (Person) constructor.newInstance(new Object[]{"Liusilong", 18}); // 这里getInfo如果是public方法的话就直接 person.getInfo(); // 如果getInfo是private方法就就还需要反射私有方法,步骤如下: Method method = c.getDeclaredMethod("getInfo"); method.setAccessible(true); method.invoke(person);
7.反射属性并输出
// 获取Class对象 Class c = Person.class; // 生成Person类的实例 Person p = (Person) c.newInstance(); // 获取Person类中的私有属性 Field name = c.getDeclaredField("name"); Field age = c.getDeclaredField("age"); // 设置私有属性可访问 name.setAccessible(true); age.setAccessible(true); // 设置Person对象中私有属性的值 name.set(p, "Liusilong"); age.set(p, 18); // 获取Person中的私有方法 getInfo Method method = c.getDeclaredMethod("getInfo"); // 设置私有方法可访问 method.setAccessible(true); // 调用私有方法 method.invoke(p);