反射(reflect):
JAVA反射机制是在运行状态中,对于任意一个实体类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意方法和属性;这种动态获取信息以及动态调用对象方法的功能称为java语言的反射机制
获取类对象:
通过查询API得知:
代码如下:
package com.baidu.reflect.test;
/**
* 测试获取类对象
* @author admin
*
*/ import static org.junit.Assert.*; import org.junit.Test; import com.baidu.reflect.domain.People; public class Test01 { @Test
public void getClass01() throws Exception {
// 实例化对象获取
People p = new People();
Class poeple = p.getClass();
System.out.println(poeple);
// com.baidu.reflect.domain.People
} @Test
public void getClass02() throws Exception {
// className.class获取
Class people = People.class;
System.out.println(people);
} @Test
public void getClass03() throws Exception {
// 完全限定名获取
Class people = Class.forName("com.baidu.reflect.domain.People");
System.out.println(people); }
}
获取构造方法:
API如下:
代码如下
package com.baidu.reflect.test; import java.lang.reflect.Constructor; import org.junit.Before;
import org.junit.Test; /**
* 通过反射获取构造方法
*
* @author admin
*
*/
public class Test02 {
// 初始化clazz对象
Class clazz = null; @Before
public void testObject() throws Exception {
// 获取类对象
clazz = Class.forName("com.baidu.reflect.domain.People");
} @Test
public void testGetConstructor01() throws Exception {
// 通过类对象获取无参构造
Constructor constructor = clazz.getConstructor();
// 通过构造获取实例
Object people = constructor.newInstance();
System.out.println(people);// People [name=null, age=0, sex=null] } @Test
public void testGetConstructor02() throws Exception {
// 无参构造可以直接获取实例
Object people = clazz.newInstance();
System.out.println(people);
} @Test
public void testGetConstructor03() throws Exception {
// 通过对象获有参构造
Constructor constructor = clazz.getConstructor(String.class, String.class);
// 获取对象的实例并设置参数
Object people = constructor.newInstance("张三", "男");
System.out.println(people);// People [name=张三, age=0, sex=男]
} @Test
public void testGetConstructor04() throws Exception {
// 通过对象获私有有参构造
Constructor constructor = clazz.getDeclaredConstructor(String.class, int.class, String.class);
// 设置访问权限
constructor.setAccessible(true);
// 获取对象的实例并设置参数
Object people = constructor.newInstance("张三", 23, "男");
System.out.println(people);// People [name=张三, age=23, sex=男]
} }
获取成员方法:
查询API得知:
代码如下
package com.baidu.reflect.test; import java.lang.reflect.Constructor;
import java.lang.reflect.Method; import org.junit.Before;
import org.junit.Test; import com.baidu.reflect.domain.People; /**
* 通过反射获取方法 对象
*
* @author admin
*
*/
public class Test03 {
// 初始化clazz对象
Class clazz = null;
People people = null; @Before
public void testObject() throws Exception {
// 获取类对象
clazz = Class.forName("com.baidu.reflect.domain.People");
// 向下转型
people = (People) clazz.newInstance();
} @Test
public void testGetMethod01() throws Exception {
// 通过类对象获取公共方法
Method setNameMethod = clazz.getMethod("setName", String.class);
// 设置方法参数
setNameMethod.invoke(people, "张三");
// 输出people [name=张三, age=0, sex=null]
System.out.println(people + "\r" + "------------");
// 同上
Method getNameMethod = clazz.getMethod("getName");
String name = (String) getNameMethod.invoke(people);
System.out.println(name);// 张三
} @Test
public void testGetMethod02() throws Exception {
// 通过类对象获取私有方法
Method getSayHiMethod = clazz.getDeclaredMethod("sayHi");
getSayHiMethod.setAccessible(true);
// 因为没有返回值:Hello reflect
getSayHiMethod.invoke(people); } }
获取成员变量:
API:
代码:
package com.baidu.reflect.test; import java.lang.reflect.Field; import org.junit.Before;
import org.junit.Test; import com.baidu.reflect.domain.People; /**
* 通过反射获取成员变量
*
* @author admin
*
*/
public class Test04 {
// 初始化clazz对象
Class clazz = null;
People people = null; @Before
public void testObject() throws Exception {
// 获取类对象
clazz = Class.forName("com.baidu.reflect.domain.People");
// 向下转型
people = (People) clazz.newInstance();
} @Test
public void testGetField01() throws Exception {
// 通过类对象获取private field;
Field nameField = clazz.getDeclaredField("name");
nameField.setAccessible(true);
nameField.set(people, "张三");
System.out.println(people);// People [name=张三, age=0, sex=null] } @Test
public void testGetField02() throws Exception {
// 通过类对象获取public field
Field sexField = clazz.getField("sex");
sexField.set(people, "未知");
System.out.println(people);// People [name=null, age=0, sex=未知] } }
实体类:
package com.baidu.reflect.domain; /**
* 实体
*
* @author admin
*
*/
public class People { private String name;
private int age;
public String sex; public People() {
} private People(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
} public People(String name, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} private void sayHi() {
System.out.println("Hello reflect");
} @Override
public String toString() {
return "People [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
}
希望能给大家一个参考,也希望大家多多支持我。
查阅文献: https://baike.baidu.com/item/JAVA反射机制/6015990?