普通开发模式下的代码是编译时已经设置好参数和属性, 反射是在运行时候动态注入参数属性
Class 类
- 在jvm 中代表 类和接口 的类
- Class 对象具体包含了某个特定类的结构信息
- 通过Class 对象可以获取到类的构造方法、方法、成员变量等
```java public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
//ClassNotFoundException 类找不到
//InstantiationException 非法访问异常
//IllegalAccessException 无法实例化异常
// 指定的类加载到jvm中, 返回对应的class 对象
Class employeeClass = Class.forName("cn.ichief.reflect.entity.Employee");
// 通过默认构造方法实例对象 默认使用无参数的 newInstance 返回的是Object 需要强制转换
Employee employee = (Employee) employeeClass.newInstance();
}
<a name="FANjH"></a>
#### Constructor 构造方法类
- ![image.png](https://cdn.nlark.com/yuque/0/2021/png/422907/1639283946710-6b7303a0-b444-45cf-b09b-a70afd8652e9.png#clientId=ud9167f32-073b-4&from=paste&height=316&id=uec2d8a47&margin=%5Bobject%20Object%5D&name=image.png&originHeight=632&originWidth=2586&originalType=binary&ratio=1&size=591295&status=done&style=none&taskId=u3735c32c-26e6-4b29-8204-f496dd79d52&width=1293)
```java
try {
Class EmployeeClass = Class.forName("cn.ichief.reflect.entity.Employee");
// 对应构造方法的参数类型
Constructor constructor = EmployeeClass.getConstructor(new Class[]{
Integer.class,
String.class,
Float.class,
String.class
});
Employee employee = (Employee) constructor.newInstance(new Object[]{
100, "ss", 3000f, "yfb"
});
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
Method 方法类
```java public static void main(String[] args) {
try {
Class EmployeeClass = Class.forName("cn.ichief.reflect.entity.Employee");
// 对应构造方法的参数类型
Constructor constructor = EmployeeClass.getConstructor(new Class[]{
Integer.class,
String.class,
Float.class,
String.class
});
Employee employee = (Employee) constructor.newInstance(new Object[]{
100, "ss", 3000f, "yfb"
});
Method updateSalaryMethod = EmployeeClass.getMethod("updateSalary", Float.class);
Employee employee1 = (Employee) updateSalaryMethod.invoke(employee, 100f);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
<a name="l2DTV"></a>
#### Field 成员变量类
- ![image.png](https://cdn.nlark.com/yuque/0/2021/png/422907/1639284803877-2be23cc2-f17a-4adb-b53c-aa8c6594c297.png#clientId=ud9167f32-073b-4&from=paste&height=642&id=ud0380cd4&margin=%5Bobject%20Object%5D&name=image.png&originHeight=1284&originWidth=2416&originalType=binary&ratio=1&size=634266&status=done&style=none&taskId=ua885e5b5-83ee-4ff2-9fa4-f5eb5da12c5&width=1208)
- ![image.png](https://cdn.nlark.com/yuque/0/2021/png/422907/1639284816663-be036d98-94f4-4d60-ad1c-1171e6979390.png#clientId=ud9167f32-073b-4&from=paste&height=437&id=u6cceaaad&margin=%5Bobject%20Object%5D&name=image.png&originHeight=874&originWidth=2702&originalType=binary&ratio=1&size=735030&status=done&style=none&taskId=u0c1031ca-b2c8-4696-a26c-8086e507cc8&width=1351)
```java
public static void main(String[] args) {
try {
Class EmployeeClass = Class.forName("cn.ichief.reflect.entity.Employee");
// 对应构造方法的参数类型
Constructor constructor = EmployeeClass.getConstructor(new Class[]{
Integer.class,
String.class,
Float.class,
String.class
});
Employee employee = (Employee) constructor.newInstance(new Object[]{
100, "ss", 3000f, "yfb"
});
Field enameField = EmployeeClass.getField("ename");
enameField.set(employee, "ssssd");
String ename = enameField.get(employee).toString();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}