概念:
什么是反射?
- 利用反射可以无视修饰符获取类里面所有的属性和方法
- 对于任何对象,都能够调用它的方法和属性,这种动态获取信息以及动态调用对象方法的功能称为Java的反射
反射的应用场景?
常见的有: idea的智能提示、框架等(通过反射技术对象类进行了解剖得到了类的所有成员。)
使用反射的步骤:
- 获取class对象
- 获取Construtor
- 创建对象
三种获取Class对象的方式:
- 类名.class
- 对象.getClass()
- Class.forName(类全名);
public class Demo01 {
public static void main(String[] args) throws ClassNotFoundException {
Class<Student> aClass = Student.class;
System.out.println(aClass);
Student student = new Student();
Class<? extends Student> aClass2 = student.getClass();
System.out.println(aClass2);
Class<?> aClass3 = Class.forName("day20.Student");
System.out.println(aClass3);
}
}
Class获取构造方法对象:
方法名 | 说明 |
---|---|
Constructor<?>[] getConstructors() | 返回所有公共 构造方法对象的数组 |
Constructor<?>[] getDeclaredConstructors() | 返回所有 构造方法对象的数组 |
Constructor getConstructor(Class<?>… parameterTypes) | 返回单个公共构造方法对象 |
Constructor getDeclaredConstructor(Class<?>… parameterTypes) | 返回单个构造方法对象 |
public class Demo02 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
Class<?> aClass = Class.forName("day20.Student");
// Constructor<?>[] getConstructors() 返回所有公共构造方法对象的数组
Constructor<?>[] constructors = aClass.getConstructors();
for (Constructor<?> constructor : constructors) {
System.out.println(constructor);
}
System.out.println("-----------------------------------");
// Constructor<?>[] getDeclaredConstructors() 返回所有构造方法对象的数组
Constructor<?>[] gdc = aClass.getDeclaredConstructors();
for (Constructor<?> constructor : gdc) {
System.out.println(constructor);
}
System.out.println("-----------------------------------");
// Constructor getConstructor(Class<?>… parameterTypes) 返回单个公共构造方法对象
//小括号中的顺序,一定要跟构造方法的形参保持一致.
Constructor<?> constructor = aClass.getConstructor();
System.out.println(constructor);
Constructor<?> constructor2 = aClass.getConstructor(String.class, int.class);
System.out.println(constructor2);
System.out.println("-----------------------------------");
// Constructor getDeclaredConstructor(Class<?>… parameterTypes) 返回单个构造方法对象,不管还是公共都可以
Constructor<?> declaredConstructor = aClass.getDeclaredConstructor();
System.out.println(declaredConstructor);
}
}
反射创建对象方法:
Constructor类作用:
表示类中的构造方法
方法名 | 说明 |
---|---|
T newInstance(Object…initargs) | 通过构造方法创建对象 |
setAccessible(boolean flag) | 设置为true,表示取消访问检查 |
public class Test02 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
// 获取Class对象
Class<?> aClass = Class.forName("day20.Student");
// 获取构造方法,有参
Constructor<?> constructor = aClass.getConstructor(String.class, int.class);
// 获取构造方法,无参
Constructor<?> constructor2 = aClass.getConstructor();
// 获取构造方法,私有
Constructor<?> declaredConstructor = aClass.getDeclaredConstructor(String.class);
// 暴力反射:用反射可以看到公共和私有的,但是被private修饰的成员是不可以直接使用的,如果要用要调用setAccessible临时取消访问检查,参数是true
declaredConstructor.setAccessible(true);
// 利用newInstance创建Student有参
Student instance = (Student) constructor.newInstance("韩信", 19);
// 利用newInstance创建Student无参
Student instance2 = (Student) constructor2.newInstance();
// 利用newInstance创建Student私有化
Student instance3 = (Student) declaredConstructor.newInstance("itzhuzhu");
System.out.println(instance);
System.out.println("------------------");
System.out.println(instance2);
System.out.println("------------------");
System.out.println(instance3);
}
}
反射获取成员变量:
Field类的作用:
表示类中的成员变量
如果获取Field:
1.获取Class对象
2.通过Class对象获取Field
方法名 | 说明 |
---|---|
Field[] getFields () | 返回所有公共成员变量对象的数组 |
Field[] getDeclaredFields() | 返回所有成员变量对象的数组 |
Field getField(String name) | 返回单个公共成员变量对象 |
Field getDeclaredField(String name) | 返回单个成员变量对象 |
public class Demo03 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
Class<?> aClass = Class.forName("day20.Student");
// Field[] getFields () 返回所有公共成员变量对象的数组
Field[] fields = aClass.getFields();
for (Field field : fields) {
System.out.println(field);
}
System.out.println("-----------------");
// Field[] getDeclaredFields() 返回所有成员变量对象的数组
Field[] declaredFields = aClass.getDeclaredFields();
for (Field declaredField : declaredFields) {
System.out.println(declaredField);
}
System.out.println("-----------------");
// Field getField(String name)返回单个公共成员变量对象,如果是private修饰的是获取不到的
Field field = aClass.getField("name");
System.out.println(field);
System.out.println("-----------------");
// Field getDeclaredField(String name) 返回单个成员变量对象,private修饰的也可以获取
Field declaredField = aClass.getDeclaredField("age");
System.out.println(declaredField);
}
}
反射赋值成员变量:
方法名 | 说明 |
---|---|
void set(Object obj, Object value) | 赋值 |
Object get(Object obj) | 获取值 |
public class Test {
public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchFieldException {
setMethod();
getMethod();
}
private static void getMethod() throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException {
Class<?> aClass = Class.forName("day20.Student");
// 获取对象fanshe.demo.Studentaaa
Field declaredField = aClass.getDeclaredField("name");
// 暴力反射
declaredField.setAccessible(true);
Student student = (Student) aClass.newInstance();
Object o = declaredField.get(student);
System.out.println(o);
}
private static void setMethod() throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException {
// 获取class对象
Class<?> aClass = Class.forName("day20.Student");
// 获取对象
Field field = aClass.getField("name");
// set赋值
Student student = (Student) aClass.newInstance();
field.set(student, "韩信");
System.out.println(student);
}
}
获取成员方法并运行:
Method类作用
表示类中的方法
如何得到Method对象
1.获取Class对象
2.通过Class对象获取Method
方法名 | 说明 |
---|---|
Method[] getMethods() | 返回所有公共成员方法对象的数组,包括继承的 |
Method[] getDeclaredMethods() | 返回所有成员方法对象的数组,不包括继承的 |
Method getMethod(String name, Class<?>… parameterTypes) | 返回单个公共成员方法对象 |
Method getDeclaredMethod(String name, Class<?>… parameterTypes) | 返回单个成员方法对象 |
Object invoke(Object obj, Object… args) | 运行方法 |
学生类:
public class Student {
//私有的,无参无返回值
private void show() {
System.out.println("私有的show方法,无参无返回值");
}
//公共的,无参无返回值
public void function1() {
System.out.println("function1方法,无参无返回值");
}
//公共的,有参无返回值
public void function2(String name) {
System.out.println("function2方法,有参无返回值,参数为" + name);
}
//公共的,无参有返回值
public String function3() {
System.out.println("function3方法,无参有返回值");
return "aaa";
}
//公共的,有参有返回值
public String function4(String name) {
System.out.println("function4方法,有参有返回值,参数为" + name);
return "aaa";
}
}
测试类:
public class Test {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
Class<?> aClass = Class.forName("day20.Student");
// Method[] getMethods() 返回所有公共成员方法对象的数组,包括继承的
Method[] methods = aClass.getMethods();
for (Method method : methods) {
System.out.println(method);
}
System.out.println("------------------");
// Method[] getDeclaredMethods() 返回所有成员方法对象的数组,不包括继承的
Method[] methods2 = aClass.getDeclaredMethods();
for (Method method : methods2) {
System.out.println(method);
}
System.out.println("------------------");
// Method getMethod(String name, Class<?>… parameterTypes) 返回单个公共成员方法对象
Method method = aClass.getMethod("function1");
Method method2 = aClass.getMethod("function2", String.class);
System.out.println(method);
System.out.println(method2);
System.out.println("------------------");
// Method getDeclaredMethod(String name, Class<?>… parameterTypes) 返回单个成员方法对象
Method show = aClass.getDeclaredMethod("show");
System.out.println(show);
System.out.println("------------------");
// Object invoke(Object obj, Object… args) 运行方法 第一个参数是调用者,第二是调用方法传递的参数
Method function4 = aClass.getMethod("function4", String.class);
// 创建调用者
Student o = (Student) aClass.newInstance();
System.out.println(function4.invoke(o,"itzhuzhu"));
}
}