如何创建仅包含已更改字段的增量对象?

发布于 2024-12-20 15:26:58 字数 157 浏览 4 评论 0原文

我必须编写一个通用实用程序类,它执行以下操作:

输入:同一类的 2 个对象 - 旧的和新的

输出:与输入相同类的 1 个对象,仅包含更改的字段 - delta

不确定是否存在任何现有的框架就在那里。我更喜欢使用 Scala,但即使是 Java 也可以。

I have to write a common utility class that does following:

Input: 2 objects of the same class - old and new

Output: 1 object of the same class as Inputs, with only the fields that change - delta

Not sure if there is any existing framework out there. I would prefer to use Scala, but even Java is ok.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我只土不豪 2024-12-27 15:26:58

如果对象是bean,那么您可以使用 java.beans API。它允许您通过 内省器。您需要迭代 objA 和 objB 的所有字段、比较值并在 objRes 上设置值。此外,类类型必须有一个默认构造函数。

If the objects are beans, then you can use the java.beans API. It lets you query what the fields of the bean are, via Introspector. It would be up to you to iterate over all the fields of objA and objB, comparing values, and setting values on objRes. Also, the class type would have to have a default constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文