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

我的意思是,如果它们是POJO,则一个类的设置者应将另一个类的获取器作为参数。

有这种情况的模式吗?

最佳答案

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/

10-10 13:06