美文网首页
Java 反射:给指定字段赋值

Java 反射:给指定字段赋值

作者: 程序员小白成长记 | 来源:发表于2021-09-27 13:22 被阅读0次
public class Student {

    private Integer id;
    private String shortId;
    private String name;

    public Student(){

    }

    public Student(Integer id, String shortId, String name) {
        this.id = id;
        this.shortId = shortId;
        this.name = name;

    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getShortId() {
        return shortId;
    }

    public void setShortId(String shortId) {
        this.shortId = shortId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
  • getDeclaredField
    包含public, protected, default范围权限的字段,不包含父类的字段
  • getField
    包含public权限的字段,包含父类的public权限的字段
public class TestSetObjectFieldValue {
    public static void main(String[] args) throws Exception {
        String[] fieldStrs = new String[]{"id", "name", "shortId"};
        Class<Student> clazz = Student.class;
        Student student = clazz.newInstance();
        for (String fieldStr : fieldStrs) {
            /**
             * Returns an array containing {@code Method} objects reflecting all the
             * declared methods of the class or interface represented by this {@code
             * Class} object, including public, protected, default (package)
             * access, and private methods, but excluding inherited methods.
             */
            Field field = clazz.getDeclaredField(fieldStr);
            /**
             * Returns a Field object that reflects the specified public member field of the class or interface
             * represented by this Class object.
             */
            // clazz.getField(fieldStr);
            // 注意
            field.setAccessible(true);
            if (field.getType().equals(Integer.class)) {
                field.set(student, 111);
            } else {
                field.set(student, "111");
            }
        }
        System.out.println(JSON.toJSONString(student));
    }
}

相关文章

  • Java 反射:给指定字段赋值

    getDeclaredField包含public, protected, default范围权限的字段,不包含父类...

  • mysql表管理

    管理表结构:1.插入 添加一条记录,给所有字段赋值 添加一条记录,指定字段赋值ps:字段值要与字段类型相匹配 字符...

  • Java反射与joor反射库的使用

    java原生反射的使用 反射构造对象 反射方法 反射字段 joor反射库的使用 github:https://gi...

  • 【Java 进阶】Java 反射

    反射:获取Class中所有字段(Field)与方法(Method),并实现调用(invoke) Java 反射简单...

  • object is not an instance of dec

    背景 在java中使用反射,将DataFromImp类的属性值赋值给DataToImp类的同名属性。代码示例如下,...

  • java反射-第3篇

    例子 通过反射动态给对象赋值

  • Java基础--反射

    什么是Java反射 概念 java反射是指java能够在运行时确定类的类型信息,包括其方法、字段、构造函数等,并能...

  • Java反射

    简单总结下什么是Java反射,怎么使用。 Java反射机制 反射就是指JVM运行期,获取指定类所有的属性和方法的一...

  • Java注解

    Java注解(Annotation)是通过在java源文件上添加标记字段,然后通过反射的反射在编译或者运行时获取这...

  • JAVA 通过反射机制给Bean对象赋值

    public classReflectGetValueUtils { /** * 取出bean 属性和值 * *@...

网友评论

      本文标题:Java 反射:给指定字段赋值

      本文链接:https://www.haomeiwen.com/subject/dtihnltx.html