在对象上按名称设置字段
//package com.nowjava;// 来自 时代Java import java.lang.reflect.Field; public class Main { public static void setField(String name, Object target, Object value) { try { Class<?> targetClass = target.getClass(); Field field = targetClass.getDeclaredField(name); boolean unlocked = false; if (!field.isAccessible()) { field.setAccessible(true); unlocked = true; } field.set(target, value); if (unlocked) { field.setAccessible(false); } } catch (