How to merge two complex objects in java

前端 未结 5 1411
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-01 23:23

I have two java objects and I want to merge them into single object. Problem is the two objects does not contain plain primitive type properties(fields) they contain complex

5条回答
  •  抹茶落季
    2021-01-01 23:48

    try to use class.getFields

        Field[] fields = YourClass.getFields();
        for (Field field : fields) {
             // get value
             YourObject value = field.get(objectInstance);
             // check the values are different, then update 
             field.set(objetInstance, value);    
        }
    

提交回复
热议问题