gpt4 book ai didi

java - 转换实现相同接口(interface)的类的快速方法

转载 作者:行者123 更新时间:2023-11-30 06:39:56 25 4
gpt4 key购买 nike

在 Java 中,如何快速(或一般地)将一个实现接口(interface)的类转换为另一个实现相同接口(interface)的类?

我的意思是,如果它们是 POJO,一个类的 setter 应该将其他类的 getter 作为参数。

这种情况有模式吗?

最佳答案

Apache Bean Utilities 包有一个工具可以做到这一点。

org.apache.commons.beanutils.BeanUtils
.BeanUtils.copyProperties

public static void copyProperties(Object dest, Object orig)
throws IllegalAccessException,
InvocationTargetException

Copy property values from the origin bean to the destination bean for all cases where the property names are the same.

For more details see BeanUtilsBean.

Parameters:
dest - Destination bean whose properties are modified
orig - Origin bean whose properties are retrieved

关于java - 转换实现相同接口(interface)的类的快速方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/235685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com