1.简介
(1).概念
反射是指程序在运行时,可以动态获取对象所有成员和方法,并且对其进行访问或修改的机制。

(2).Class类

  • 类是用来描述事物的,那么描述类的类就是Class类。
  • Class类的对象只能由JVM创建,JVM中只有唯一一个和类相对应的Class对象。

(3).reflect类库

  • Field:表示类中的成员变量
  • Method:表示类中的成员方法
  • Constructor:表示类中的构造方法

(4).演示类

public class Student {
private String name;

int age;

public Student() {
System.out.println("constructor run without params");
}

private Student(String name) {
this.name = name;
System.out.println("public constructor params run with name:" + this.name);
}

public Student(String name, int age) {
this.name = name;
this.age = age;
System.out.println("public constructor params run with name:" + this.name + ",age:" + this.age);
}

public void show() {
System.out.println("public show run");
}

private void show1() {
System.out.println("private show run");
}

public void fun(String name) {
System.out.println("fun run with name:" + name);
}

public static void staticMethod() {
System.out.println("staticMethod run");
}

@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}

2.获取Class对象的三种方式
(1).getClass()

public class ReflectTarget {
public static void main(String[] args) {
ReflectTarget reflectTarget = new ReflectTarget();
Class reflectTargetClass = reflectTarget.getClass();
System.out.println(reflectTargetClass.getName());
}
}

(2).class属性

public class ReflectTarget {
public static void main(String[] args) {
Class reflectTargetClass = ReflectTarget.class;
System.out.println(reflectTargetClass.getName());
}
}

(3).forName()

public class ReflectTarget {
public static void main(String[] args) throws ClassNotFoundException {
Class reflectTargetClass = Class.forName("com.steven.reflect.ReflectTarget");
System.out.println(reflectTargetClass.getName());
}
}

3.获取构造方法
(1).无参

public class ReflectTarget {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class studentClass = Class.forName("com.steven.reflect.Student");
Constructor constructor = studentClass.getConstructor();
Student student = (Student)constructor.newInstance();
student.show();
}
}
constructor run without params
public show run

(2).公有含参数

public class ReflectTarget {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class studentClass = Class.forName("com.steven.reflect.Student");
Constructor constructor = studentClass.getDeclaredConstructor(String.class,int.class);
Student student = (Student)constructor.newInstance("steven",30);
System.out.println(student);
}
}
public constructor params run with name:steven,age:30
Student{name='steven', age=30}

(3).私有含参数

public class ReflectTarget {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class studentClass = Class.forName("com.steven.reflect.Student");
Constructor constructor = studentClass.getDeclaredConstructor(String.class);
constructor.setAccessible(true);
Student student = (Student)constructor.newInstance("steven");
System.out.println(student);
}
}
public constructor params run with name:steven
Student{name='steven', age=0}

4.获取成员变量
(1).公有成员变量

public class ReflectTarget {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InstantiationException, InvocationTargetException {
Class studentClass = Class.forName("com.steven.reflect.Student");
Student student = (Student)studentClass.newInstance();
Field field = studentClass.getDeclaredField("age");
field.set(student,30);
System.out.println(student);
}
}
constructor run without params
Student{name='null', age=30}

(2).私有成员变量

public class ReflectTarget {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InstantiationException, InvocationTargetException {
Class studentClass = Class.forName("com.steven.reflect.Student");
Student student = (Student)studentClass.newInstance();
Field field = studentClass.getDeclaredField("name");
field.setAccessible(true);
field.set(student,"steven");
System.out.println(student);
}
}
constructor run without params
Student{name='steven', age=0}

5.获取成员方法
(1).公有成员方法

Class studentClass = Class.forName("com.steven.reflect.Student");
Student student = (Student)studentClass.newInstance();
Method method = studentClass.getDeclaredMethod("fun",String.class);
method.invoke(student,"steven");
constructor run without params
fun run with name:steven

(2).私有成员方法

public class ReflectTarget {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InstantiationException, InvocationTargetException {
Class studentClass = Class.forName("com.steven.reflect.Student");
Student student = (Student)studentClass.newInstance();
Method method = studentClass.getDeclaredMethod("show1");
method.setAccessible(true);
method.invoke(student);
}
}
constructor run without params
private show run