反射(reflect):
JAVA反射机制是在运行状态中,对于任意一个实体类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意方法和属性;这种动态获取信息以及动态调用对象方法的功能称为java语言的反射机制
获取类对象:
通过查询API得知:
代码如下:
1 package com.baidu.reflect.test; 2 /** 3 * 测试获取类对象 4 * @author admin 5 * 6 */ 7 8 import static org.junit.Assert.*; 9 10 import org.junit.Test; 11 12 import com.baidu.reflect.domain.People; 13 14 public class Test01 { 15 16 @Test 17 public void getClass01() throws Exception { 18 // 实例化对象获取 19 People p = new People(); 20 Class poeple = p.getClass(); 21 System.out.println(poeple); 22 // com.baidu.reflect.domain.People 23 } 24 25 @Test 26 public void getClass02() throws Exception { 27 // className.class获取 28 Class people = People.class; 29 System.out.println(people); 30 } 31 32 @Test 33 public void getClass03() throws Exception { 34 // 完全限定名获取 35 Class people = Class.forName("com.baidu.reflect.domain.People"); 36 System.out.println(people); 37 38 } 39 }
获取构造方法:
API如下:
代码如下
1 package com.baidu.reflect.test; 2 3 import java.lang.reflect.Constructor; 4 5 import org.junit.Before; 6 import org.junit.Test; 7 8 /** 9 * 通过反射获取构造方法 10 * 11 * @author admin 12 * 13 */ 14 public class Test02 { 15 // 初始化clazz对象 16 Class clazz = null; 17 18 @Before 19 public void testObject() throws Exception { 20 // 获取类对象 21 clazz = Class.forName("com.baidu.reflect.domain.People"); 22 } 23 24 @Test 25 public void testGetConstructor01() throws Exception { 26 // 通过类对象获取无参构造 27 Constructor constructor = clazz.getConstructor(); 28 // 通过构造获取实例 29 Object people = constructor.newInstance(); 30 System.out.println(people);// People [name=null, age=0, sex=null] 31 32 } 33 34 @Test 35 public void testGetConstructor02() throws Exception { 36 // 无参构造可以直接获取实例 37 Object people = clazz.newInstance(); 38 System.out.println(people); 39 } 40 41 @Test 42 public void testGetConstructor03() throws Exception { 43 // 通过对象获有参构造 44 Constructor constructor = clazz.getConstructor(String.class, String.class); 45 // 获取对象的实例并设置参数 46 Object people = constructor.newInstance("张三", "男"); 47 System.out.println(people);// People [name=张三, age=0, sex=男] 48 } 49 50 @Test 51 public void testGetConstructor04() throws Exception { 52 // 通过对象获私有有参构造 53 Constructor constructor = clazz.getDeclaredConstructor(String.class, int.class, String.class); 54 // 设置访问权限 55 constructor.setAccessible(true); 56 // 获取对象的实例并设置参数 57 Object people = constructor.newInstance("张三", 23, "男"); 58 System.out.println(people);// People [name=张三, age=23, sex=男] 59 } 60 61 }
获取成员方法:
查询API得知:
代码如下
1 package com.baidu.reflect.test; 2 3 import java.lang.reflect.Constructor; 4 import java.lang.reflect.Method; 5 6 import org.junit.Before; 7 import org.junit.Test; 8 9 import com.baidu.reflect.domain.People; 10 11 /** 12 * 通过反射获取方法 对象 13 * 14 * @author admin 15 * 16 */ 17 public class Test03 { 18 // 初始化clazz对象 19 Class clazz = null; 20 People people = null; 21 22 @Before 23 public void testObject() throws Exception { 24 // 获取类对象 25 clazz = Class.forName("com.baidu.reflect.domain.People"); 26 // 向下转型 27 people = (People) clazz.newInstance(); 28 } 29 30 @Test 31 public void testGetMethod01() throws Exception { 32 // 通过类对象获取公共方法 33 Method setNameMethod = clazz.getMethod("setName", String.class); 34 // 设置方法参数 35 setNameMethod.invoke(people, "张三"); 36 // 输出people [name=张三, age=0, sex=null] 37 System.out.println(people + "\r" + "------------"); 38 // 同上 39 Method getNameMethod = clazz.getMethod("getName"); 40 String name = (String) getNameMethod.invoke(people); 41 System.out.println(name);// 张三 42 } 43 44 @Test 45 public void testGetMethod02() throws Exception { 46 // 通过类对象获取私有方法 47 Method getSayHiMethod = clazz.getDeclaredMethod("sayHi"); 48 getSayHiMethod.setAccessible(true); 49 // 因为没有返回值:Hello reflect 50 getSayHiMethod.invoke(people); 51 52 } 53 54 }
获取成员变量:
API:
代码:
1 package com.baidu.reflect.test; 2 3 import java.lang.reflect.Field; 4 5 import org.junit.Before; 6 import org.junit.Test; 7 8 import com.baidu.reflect.domain.People; 9 10 /** 11 * 通过反射获取成员变量 12 * 13 * @author admin 14 * 15 */ 16 public class Test04 { 17 // 初始化clazz对象 18 Class clazz = null; 19 People people = null; 20 21 @Before 22 public void testObject() throws Exception { 23 // 获取类对象 24 clazz = Class.forName("com.baidu.reflect.domain.People"); 25 // 向下转型 26 people = (People) clazz.newInstance(); 27 } 28 29 @Test 30 public void testGetField01() throws Exception { 31 // 通过类对象获取private field; 32 Field nameField = clazz.getDeclaredField("name"); 33 nameField.setAccessible(true); 34 nameField.set(people, "张三"); 35 System.out.println(people);// People [name=张三, age=0, sex=null] 36 37 } 38 39 @Test 40 public void testGetField02() throws Exception { 41 // 通过类对象获取public field 42 Field sexField = clazz.getField("sex"); 43 sexField.set(people, "未知"); 44 System.out.println(people);// People [name=null, age=0, sex=未知] 45 46 } 47 48 }
实体类:
1 package com.baidu.reflect.domain; 2 3 /** 4 * 实体 5 * 6 * @author admin 7 * 8 */ 9 public class People { 10 11 private String name; 12 private int age; 13 public String sex; 14 15 public People() { 16 } 17 18 private People(String name, int age, String sex) { 19 this.name = name; 20 this.age = age; 21 this.sex = sex; 22 } 23 24 public People(String name, String sex) { 25 this.name = name; 26 this.age = age; 27 this.sex = sex; 28 } 29 30 public String getName() { 31 return name; 32 } 33 34 public void setName(String name) { 35 this.name = name; 36 } 37 38 public int getAge() { 39 return age; 40 } 41 42 public void setAge(int age) { 43 this.age = age; 44 } 45 46 public String getSex() { 47 return sex; 48 } 49 50 public void setSex(String sex) { 51 this.sex = sex; 52 } 53 54 private void sayHi() { 55 System.out.println("Hello reflect"); 56 } 57 58 @Override 59 public String toString() { 60 return "People [name=" + name + ", age=" + age + ", sex=" + sex + "]"; 61 } 62 }
希望能给大家一个参考,也希望大家多多支持我。
查阅文献: https://baike.baidu.com/item/JAVA反射机制/6015990?