package com.itheima.myreffect3;
import java.lang.reflect.Field;
public class RefectDemo2 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException {
//method1();
method2();
}
private static void method2() throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException {
Class clazz = Class.forName("com.itheima.myreffect3.Student");
Field field = clazz.getDeclaredField("money");
field.setAccessible(true);
Student student = (Student) clazz.newInstance();
Object o = field.get(student);
System.out.println(o);
}
private static void method1() throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException {
Class clazz = Class.forName("com.itheima.myreffect3.Student");
154.-反射-利用Field赋值和获取值__
最新推荐文章于 2024-01-24 16:08:20 发布