ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
~~~ Class<?> aClass = Class.forName("domain.Person"); // 所有属性 Field[] declaredFields = aClass.getDeclaredFields(); for (Field field : declaredFields){ System.out.println(field.getName()); } // 所有方法 Method[] declaredMethods = aClass.getDeclaredMethods(); for (Method method : declaredMethods){ System.out.println(method.getName()); } // 初始化对象 Person p = (Person)aClass.newInstance(); Field nameField = aClass.getDeclaredField("name"); // 标记为true, 允许反射赋值 nameField.setAccessible(true); nameField.set(p, "test"); System.out.println(p.getName()); ~~~