簡體   English   中英

轉換實現相同接口的類的快速方法

[英]Quick method to convert classes implementing same interface

在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 

我相信這種情況的模式稱為Proxy

在最一般的形式上,代理是一個類,它充當另一件事的接口。 另一件事可能是任何東西:網絡連接,內存中的大對象,文件或其他昂貴或無法復制的其他資源。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM