public class CopyUtils {

//部分copy 需要传属性名称
 public static void copyAttribute(Object objSrc,Object objTar,String... attributes){
  for(String attribute:attributes){
   try{
    Field f = objSrc.getClass().getDeclaredField(attribute);
    f.setAccessible(true);
    f.set(objTar,f.get(objSrc));
    f.setAccessible(false);
   } catch (Exception e) {
    //没有该方法
   }
  }
 }

//全部copy
 public static void copyAttributeAll(Object objSrc,Object objTar){
  Field[] fields = objSrc.getClass().getDeclaredFields();
  for(Field f:fields){
   try{
    f.setAccessible(true);
    f.set(objTar,f.get(objSrc));
    f.setAccessible(false);
   } catch (Exception e) {
    //没有该方法
   }
  }
 }
 }

posted on 2014-03-18 14:19  你猜呢  阅读(1181)  评论(0编辑  收藏  举报