在运行状态中,对任意一个类,都能够直到这个类的所有属性和方法
对于任意一个对象,都能够调用它的任意属性和方法
这种动态获取信息以及动态调用对象方法的功能称为Java语言的反射机制
利用反射调用它类中的属性和方法时,无视修饰符
就是说:
利用反射可以无视修饰符获取类里面所有的属性和方法
先获取配置文件中的信息,动态获取信息并创建对象和调用方法
package com.myreflect;
/**
* 获取class对象的三种方式
*/
public class ReflectDemo1 {
public static void main(String[] args) throws ClassNotFoundException {
//Class类中的静态方法forName("全类名")
//全类名 包名+类名
Class clazz= Class.forName("com.myreflect.Student");
System.out.println(clazz);
//通过class属性来获取
Class clazz2 = Student.class;
System.out.println(clazz2);
//利用对象的getClass方法获取class对象
//getClass方法是定义在Object类中
Student s = new Student();
Class clazz3 = s.getClass();
System.out.println(clazz3);
System.out.println(clazz == clazz2);
System.out.println(clazz3 == clazz2);
}
}
package com.myreflect2;
import java.lang.reflect.Constructor;
public class ReflectDemo1 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
// method1();
// method2();
//method3();
//method4();
}
private static void method4() throws ClassNotFoundException, NoSuchMethodException {
//返回单个构造方法对象
Class clazz = Class.forName("com.myreflect2.Student");
Constructor constructor = clazz.getDeclaredConstructor(String.class);
System.out.println(constructor);
}
private static void method3() throws ClassNotFoundException, NoSuchMethodException {
//返回单个公共构造方法对象
Class clazz = Class.forName("com.myreflect2.Student");
//小括号中一定要跟构造方法的形参保持一致
Constructor constructor = clazz.getConstructor(String.class, int.class);
Constructor constructor1 = clazz.getConstructor();
System.out.println(constructor1);
System.out.println(constructor);
}
private static void method2() throws ClassNotFoundException {
//返回所有构造方法对象的数组
Class clazz = Class.forName("com.myreflect2.Student");
Constructor[] constructors = clazz.getDeclaredConstructors();
for (Constructor constructor : constructors) {
System.out.println(constructor);
}
}
private static void method1() throws ClassNotFoundException {
//返回所有公共构造方法对象的数组
/**
* 获取Class对象
*/
Class clazz = Class.forName("com.myreflect2.Student");
Constructor[] constructors = clazz.getConstructors();
for (Constructor constructor : constructors) {
System.out.println(constructor);
}
}
}
package com.myreflect3;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
/**
* 获取Contructor对象并创建对象
*/
public class ReflectDemo2 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
//newInstance (Object... initargs) 根据指定的构造方法创建对象
//method1();
//method2();
//method3();
}
private static void method3() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
//获取一个私有的构造方法并创建对象
Class clazz = Class.forName("com.myreflect3.Student");
//获取一个私有化的构造方法
Constructor constructor = clazz.getDeclaredConstructor(String.class);
/**
* 被private 修饰的成员,不能直接使用
* 如果用反射强行获取并使用,需要临时取消访问检查
*/
constructor.setAccessible(true);
//创建对象
Student student = (Student) constructor.newInstance("zhangsan");
}
private static void method2() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
//获取对象
Class clazz = Class.forName("com.myreflect3.Student");
//获取构造方法对象
Constructor constructor = clazz.getConstructor();
//利用空参来创建Student的对象
Student student = (Student) constructor.newInstance();
System.out.println(student);
}
private static void method1() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
//获取class对象
Class clazz = Class.forName("com.myreflect3.Student");
//获取构造方法对象
Constructor constructor = clazz.getConstructor(String.class, int.class);
//利用newInstace 创建Student对象
Student student = (Student) constructor.newInstance("zhangsan", 23);
System.out.println(student);
}
}
如果是非public,需要临时取消检查,然后再创建对象
setAccessible(boolean) 暴力反射
package com.myreflect4;
import java.lang.reflect.Field;
public class ReflectDemo1 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
//method();
//method1();
//method2();
//method3();
}
private static void method3() throws ClassNotFoundException, NoSuchFieldException {
//返回单个成员变量对象
Class clazz = Class.forName("com.myreflect4.Student");
//获取money成员变量
Field field = clazz.getDeclaredField("money");
System.out.println(field);
}
private static void method2() throws ClassNotFoundException, NoSuchFieldException {
//获取class对象
Class clazz = Class.forName("com.myreflect4.Student");
//获取name这个成员变量
/**
* 获取的成员变量必须真实存在
* 并且是Public修饰的
*/
Field field = clazz.getField("name");
//
System.out.println(field);
}
private static void method1() throws ClassNotFoundException {
//返回所有成员变量对象的数组
//获取class对象
Class clazz = Class.forName("com.myreflect4.Student");
//获取所有field对象
Field[] fields = clazz.getDeclaredFields();
//便利
for (Field field : fields) {
System.out.println(field);
}
}
private static void method() throws ClassNotFoundException {
//返回所有公共成员变量对象的数组
//获取class对象
Class clazz = Class.forName("com.myreflect4.Student");
//获取Field对象
Field[] fields = clazz.getFields();
//遍历
for (Field field : fields) {
System.out.println(field);
}
}
}
package com.myreflect5;
import java.lang.reflect.Field;
public class ReflectDemo1 {
public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchFieldException, ClassNotFoundException {
//method1();
//method2();
}
private static void method2() throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException {
//获取class对象
Class clazz = Class.forName("com.myreflect5.Student");
//获取成员变量Field的对象
Field field = clazz.getDeclaredField("money");
//取消访问检查
field.setAccessible(true);
//调用get方法来获取值
//创建一个对象
Student student = (Student) clazz.newInstance();
//获取指定对象money的值
Object o = field.get(student);
System.out.println(o);
}
private static void method1() throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException {
/**
* 给obj对象的成员方法赋值
*/
//获取class对象
Class clazz = Class.forName("com.myreflect5.Student");
//获取name这个Field对象
Field field = clazz.getField("name");
/**
* 利用set方法进行赋值
* 先创建一个Student对象
*
*/
Student student = (Student) clazz.newInstance();
//有了对象才可以给指定对象进行赋值
field.set(student, "zhangsan");
System.out.println(student);
}
}
package com.myreflect6;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class ReflectDemo1 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
//method1();
//method2();
//method3();
//method4();
//method5();
}
private static void method5() throws ClassNotFoundException, NoSuchMethodException {
//返回单个成员方法对象
Class clazz = Class.forName("com.myreflect6.Student");
//获取一个成员方法show
Method show = clazz.getDeclaredMethod("show");
System.out.println(show);
}
private static void method4() throws ClassNotFoundException, NoSuchMethodException {
//获取class对象
Class clazz = Class.forName("com.myreflect6.Student");
//获取一个有形参的方法funtion2
Method method = clazz.getMethod("function2", String.class);
System.out.println(method);
}
private static void method3() throws ClassNotFoundException, NoSuchMethodException {
//返回单个公共成员方法
Class clazz = Class.forName("com.myreflect6.Student");
//获取成员方法function1
Method method = clazz.getMethod("function1");
//
System.out.println(method);
}
private static void method2() throws ClassNotFoundException {
//返回所有成员方法对象的数组,不含继承
Class clazz = Class.forName("com.myreflect6.Student");
//获取Method方法
Method[] methods = clazz.getDeclaredMethods();
//遍历
for (Method method : methods) {
System.out.println(method);
}
}
private static void method1() throws ClassNotFoundException {
//返回所有公共成员方法对象的数组,包扩继承
//获取class对象
Class clazz = Class.forName("com.myreflect6.Student");
//获取成员方法对象
Method[] methods = clazz.getMethods();
//遍历
for (Method method : methods) {
System.out.println(method);
}
}
}
package com.myreflect6;
/**
* 获取Field 对象
*/
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";
}
}
package com.myreflect7;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ReflectDemo1 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
//获取class对象
Class clazz = Class.forName("com.myreflect7.Student");
//获取Method对象 function4
Method method = clazz.getMethod("function4", String.class);
/**
* 运行function4
* 创建Student对象,当做方法调用者
*/
Student student = (Student) clazz.newInstance();
//运行方法
Object rusult = method.invoke(student, "zhangsan");
System.out.println(rusult);
}
}