通过反射设置字段值

import java.lang.reflect.Field;

public class FieldTrouble {
    public Integer val;

    public static void main(String... args) {
        FieldTrouble ft = new FieldTrouble();
        try {
            Class c = ft.getClass();
            Field f = c.getDeclaredField("val");
            f.setInt(ft, 42); // IllegalArgumentException

            // production code should handle these exceptions more gracefully
        } catch (NoSuchFieldException x) {
            x.printStackTrace();
        } catch (IllegalAccessException x) {
            x.printStackTrace();
        }
    }
}

你可能感兴趣的:(通过反射设置字段值)