Java反射机制
一、反射
1.反射的概念
灵活调用
2.获取class对象
public class Student {
private String name;
private int age;
public Student() {
}
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 void study(){
System.out.println("学生正在学习");
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
/**
* 获取class对象的三种方式
*
*/
public class ReflectDemo1 {
public static void main(String[] args) throws ClassNotFoundException {
//1.Class类中的静态方法forName("全类名")
//全类名:包名+类名
Class cl1= Class.forName("com.jou.MyReflectDemo.Student");
System.out.println(cl1);
//2.通过class属性来获取
Class cl2=Student.class;
System.out.println(cl2);
//3.利用对象的getClass对象获取class对象
//getClass方法定义再Object类中
Student stu=new Student();
Class<? extends Student> cl3 = stu.getClass();
System.out.println(cl3);
System.out.println(cl1==cl2);
System.out.println(cl2==cl3);
}
}
3. 获取Constructor对象
public class Student {
private String name;
private int age;
//私有的有参构造
private Student(String name) {
System.out.println("name的值为:"+name);
System.out.println("private ..Student.. 有参构造方法");
}
//公共的无参构造方法
public Student() {
System.out.println("public ..Student.. 无参构造方法");
}
//公共的有参构造方法
public Student(String name, int age) {
System.out.println("name的值为:"+name+"age的值为:"+age);
System.out.println("public ..Student.. 有参构造方法");
}
}
import java.lang.reflect.Constructor;
/**
* 获取Constructor对象
*/
public class ReflectDemo1 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
//ctrl+alt+m抽取成一个方法
//method1();
//method2();
//method3();
//method4();
}
private static void method4() throws ClassNotFoundException, NoSuchMethodException {
//1.获取class对象
Class<?> aClass = Class.forName("com.jou.MyReflectDemo.Student");
//getDeclaredConstructor 获得private的对象
Constructor<?> declaredConstructor = aClass.getDeclaredConstructor(String.class);
System.out.println(declaredConstructor);
/*** output:
* private com.jou.MyReflectDemo.Student(java.lang.String)
*/
}
private static void method3() throws ClassNotFoundException, NoSuchMethodException {
//1.获取class对象
Class<?> aClass = Class.forName("com.jou.MyReflectDemo.Student");
//2.括号中,与构造方法形参保持一致 获得指定的对象
//空参
Constructor<?> constructor1 = aClass.getConstructor();
System.out.println(constructor1);
//含参
Constructor<?> constructor2 = aClass.getConstructor(String.class,int.class);
System.out.println(constructor2);
/*** output
* public com.jou.MyReflectDemo.Student()
* public com.jou.MyReflectDemo.Student(java.lang.String,int)
*/
}
private static void method2() throws ClassNotFoundException {
//1.获取class对象
Class<?> aClass = Class.forName("com.jou.MyReflectDemo.Student");
//2.getDeclaredConstructors() 返回所有构造方法对象的数组 public and private
Constructor<?>[] declaredConstructors = aClass.getDeclaredConstructors();
for (Constructor<?> declaredConstructor : declaredConstructors) {
System.out.println(declaredConstructor);
}
/***output:
* private com.jou.MyReflectDemo.Student(java.lang.String)
* public com.jou.MyReflectDemo.Student()
* public com.jou.MyReflectDemo.Student(java.lang.String,int)
*/
}
private static void method1() throws ClassNotFoundException {
//1.获取class对象
Class<?> aClass = Class.forName("com.jou.MyReflectDemo.Student");
//2.getConstructors() 返回所有**公共**构造方法对象的数组 public修饰的
Constructor<?>[] constructors = aClass.getConstructors();
for (Constructor<?> constructor : constructors) {
System.out.println(constructor);
}
/***output:
* public com.jou.MyReflectDemo.Student()
* public com.jou.MyReflectDemo.Student(java.lang.String,int)
*/
}
4. 利用Constructor创建对象
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
/**
* 利用Constructor创建对象
*/
public class ReflectDemo2 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
// method1();
// method2();
// method3();
// method4();
}
private static void method4() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
//获取一个私有的构造方法并创建对象
//1.获取Class对象
Class<?> aClass = Class.forName("com.jou.MyReflectDemo.Student");
//2.获取一个私有化的构造方法
Constructor<?> constructor = aClass.getDeclaredConstructor(String.class);
//被private修饰的成员不能被直接使用,如果要使用反射强行获取并使用,需要临时取消访问检查
constructor.setAccessible(true);
//3.创建对象
Student student = (Student) constructor.newInstance("zhangsan");
System.out.println(student);
}
private static void method3() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
//简写格式
//1.获取Class对象
Class<?> aClass = Class.forName("com.jou.MyReflectDemo.Student");
//2.Class类中newInstance方法,可以利用空参直接创建对象
Student student = (Student) aClass.newInstance();
System.out.println(student);
}
private static void method2() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
//1.获取Class对象
Class<?> aClass = Class.forName("com.jou.MyReflectDemo.Student");
//2.获取构造方法对象
Constructor<?> constructor = aClass.getConstructor();
Student student = (Student) constructor.newInstance();
System.out.println(student);
}
private static void method1() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
//1.获取Class对象
Class<?> aClass = Class.forName("com.jou.MyReflectDemo.Student");
//2.获取构造方法对象
Constructor<?> constructor = aClass.getConstructor(String.class, int.class);
//3.利用 创建Student对象
Student zhangsan = (Student) constructor.newInstance("zhangsan", 18);
System.out.println(zhangsan);
}
5.反射获取构造方法流程
6.反射获取成员方法流程
7.反射获取成员对象
import java.lang.reflect.Field;
/**
* 获取Field对象
*/
public class ReflectDemo1 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
//method1();
//method2();
//method3();
}
private static void method4() throws ClassNotFoundException, NoSuchFieldException {
//1.获取class对象
Class<?> aClass = Class.forName("com.jou.MyReflectDemo.Student");
//getField() 返回**单个成员变量对象** private and public都可以
Field money = aClass.getDeclaredField("money");
System.out.println(money);
/*** output:
* private int com.jou.MyReflectDemo.Student.money
*/}
private static void method3() throws ClassNotFoundException, NoSuchFieldException {
//1.获取class对象
Class<?> aClass = Class.forName("com.jou.MyReflectDemo.Student");
//getField() 返回**单个公共(public)成员变量** public
Field name = aClass.getField("name");
System.out.println(name);
/*** output:
* public java.lang.String com.jou.MyReflectDemo.Student.name
*/}
private static void method2() throws ClassNotFoundException {
//1.获取class对象
Class<?> aClass = Class.forName("com.jou.MyReflectDemo.Student");
//Field[] getDeclaredFields() 返回**所有成员**变量对象的数组 public and private
Field[] declaredFields = aClass.getDeclaredFields();
for (Field declaredField : declaredFields) {
System.out.println(declaredField);
}
/** output:
* public java.lang.String com.jou.MyReflectDemo.Student.name
* public int com.jou.MyReflectDemo.Student.age
* public java.lang.String com.jou.MyReflectDemo.Student.gender
* private int com.jou.MyReflectDemo.Student.money
*/}
private static void method1() throws ClassNotFoundException {
//1.获取class对象
Class<?> aClass = Class.forName("com.jou.MyReflectDemo.Student");
//Field[] getFields() 返回所有公共成员变量对象的数组 **public修饰的变量**
Field[] fields = aClass.getFields();
for (Field field : fields) {
System.out.println(field);
}
/** output:
* public java.lang.String com.jou.MyReflectDemo.Student.name
* public int com.jou.MyReflectDemo.Student.age
* public java.lang.String com.jou.MyReflectDemo.Student.gender
*/
}
}
8.利用Field赋值和获取值
import java.lang.reflect.Field;
/***
* 利用Field对象,获取值或修改值
*/
public class ReflectDemo1 {
public static void main(String[] args) throws NoSuchFieldException, ClassNotFoundException, InstantiationException, IllegalAccessException {
//method1();
//method2();
}
private static void method2() throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException {
//1.获取class对象
Class<?> aClass = Class.forName("com.jou.MyReflectDemo.Student");
//2.获取name这个Field对象
Field field = aClass.getDeclaredField("money");
field.setAccessible(true);
Student student = (Student) aClass.newInstance();
Object o = field.get(student);
System.out.println(o);
/*** output:
* 500
*/}
private static void method1() throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException {
//1.获取class对象
Class<?> aClass = Class.forName("com.jou.MyReflectDemo.Student");
//2.获取name这个Field对象
Field field = aClass.getField("name");
//3.利用set方法赋值
//3.1先创建一个Student对象
Student student = (Student) aClass.newInstance();
field.set(student,"zfz");
System.out.println(student.toString());
/***output:
* Student{name='zfz', age=0, gender='null', money=500}
*/}
}
9.获取method对象
import java.lang.reflect.Method;
public class ReflectDemo2 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
//method1();
//method2();
//method3();
//method4();
//method5();
}
private static void method5() throws ClassNotFoundException, NoSuchMethodException {
//1.获取class对象
Class<?> aClass = Class.forName("com.jou.MyReflectDemo.Student");
//2.getMethod 返回单个公共成员(public)方法对象
Method method = aClass.getDeclaredMethod("show");
System.out.println(method);
/**output:
*private void com.jou.MyReflectDemo.Student.show()
*/}
private static void method4() throws ClassNotFoundException, NoSuchMethodException {
//1.获取class对象
Class<?> aClass = Class.forName("com.jou.MyReflectDemo.Student");
//2.getMethod() 返回单个公共成员(public)方法对象
Method function2 = aClass.getMethod("function2",String.class);
System.out.println(function2);
/**output:
* public void com.jou.MyReflectDemo.Student.function2(java.lang.String)
*/}
private static void method3() throws ClassNotFoundException, NoSuchMethodException {
//1.获取class对象
Class<?> aClass = Class.forName("com.jou.MyReflectDemo.Student");
//2.getMethod() 返回单个公共成员(public)方法对象
Method function1 = aClass.getMethod("function1");
System.out.println(function1);
/**output:
* public void com.jou.MyReflectDemo.Student.function1()
*/}
private static void method2() throws ClassNotFoundException {
//1.获取class对象
Class<?> aClass = Class.forName("com.jou.MyReflectDemo.Student");
//2. Method[] getDeclaredMethods() 返回所有成员(private and public)方法对象的数组,不包括继承的
Method[] methods = aClass.getDeclaredMethods();
for (Method method : methods) {
System.out.println(method);
}
/**output:
* private void com.jou.MyReflectDemo.Student.show()
* public void com.jou.MyReflectDemo.Student.function1()
* public java.lang.String com.jou.MyReflectDemo.Student.function3()
* public java.lang.String com.jou.MyReflectDemo.Student.function4(java.lang.String)
* public void com.jou.MyReflectDemo.Student.function2(java.lang.String)
*/}
private static void method1() throws ClassNotFoundException {
//1.获取class对象
Class<?> aClass = Class.forName("com.jou.MyReflectDemo.Student");
//2. Method[] getMethods() 返回所有公共成员方法对象的数组,包括继承的
Method[] methods = aClass.getMethods();
for (Method method : methods) {
System.out.println(method);
}
/** output:
* public java.lang.String com.jou.MyReflectDemo.Student.function3()
* public void com.jou.MyReflectDemo.Student.function2(java.lang.String)
* public java.lang.String com.jou.MyReflectDemo.Student.function4(java.lang.String)
* public void com.jou.MyReflectDemo.Student.function1()
* public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
* public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
* public final void java.lang.Object.wait() throws java.lang.InterruptedException
* public boolean java.lang.Object.equals(java.lang.Object)
* public java.lang.String java.lang.Object.toString()
* public native int java.lang.Object.hashCode()
* public final native java.lang.Class java.lang.Object.getClass()
* public final native void java.lang.Object.notify()
* public final native void java.lang.Object.notifyAll()
*/}
}
10.利用method对象运行方法
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ReflectDemo3 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
//1.获取class对象
Class<?> aClass = Class.forName("com.jou.MyReflectDemo.Student");
Method method = aClass.getDeclaredMethod("function4", String.class);
//Object invoke()
// 参数一:用obj对象调用该方法
// 参数二:调用方法传递的参数(没有就不写)
//返回值:方法的返回值(没有就不写)
//创建一个Student对象,当作方法的调用者
Student student = (Student) aClass.newInstance();
Object result = method.invoke(student, "zfz");
System.out.println(result);
/**output:
* function4()有参有返回值的,参数为zfz
* Aaaa
*/
}
}