1.对象person
public class Person { private long id; private String name; private List<Child> childs=new ArrayList<Child>(); private Date cyrzDate; private Double age; private Child child; get{} set{} }
2.对象Child
public class Child { private long id; private String name; }
3.测试
public static void main(String[] args) throws Exception { SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd"); Person personZhangsan=new Person(); personZhangsan.setName("zhansan"); Calendar calendar=Calendar.getInstance(); calendar.set(2014, 10, 16); personZhangsan.setCyrzDate(calendar.getTime()); personZhangsan.setAge(20d); personZhangsan.getChilds().add(new Child(10, "zhangsan_child10")); personZhangsan.setChild(new Child(1, "child_zhangsan")); Person personLisi=new Person(); personLisi.setName("lisi"); personLisi.setAge(40d); personLisi.setName("wo jiao li si"); personLisi.setChild(new Child(1, "child_lisi")); calendar.set(2015, 10, 16); personLisi.setCyrzDate(calendar.getTime()); BeanUtils.copyProperties(personLisi, personZhangsan); personZhangsan.setName("zhangsan2"); personZhangsan.setAge(10d); personZhangsan.setCyrzDate(calendar.getTime()); personZhangsan.getChilds().add(new Child(11, "zhangsan_child11")); personZhangsan.getChild().setName("child_zhangsan2"); //zhangsan2||child_zhangsan2||10.0||2015-11-16||2 System.out.println(personZhangsan.getName()+"||"+personZhangsan.getChild().getName()+"||"+personZhangsan.getAge()+"||"+dateFormat.format(personZhangsan.getCyrzDate())+"||"+personZhangsan.getChilds().size()); //zhansan||child_zhangsan2||20.0||2014-11-16||2 System.out.println(personLisi.getName()+"||"+personLisi.getChild().getName()+"||"+personLisi.getAge()+"||"+dateFormat.format(personLisi.getCyrzDate())+"||"+personLisi.getChilds().size()); personLisi.getChild().setName("child_lisi2"); //修改lisi孩子的名字影响zhangsan孩子的名字 //zhangsan2||child_lisi2||10.0||2015-11-16||2 System.out.println(personZhangsan.getName()+"||"+personZhangsan.getChild().getName()+"||"+personZhangsan.getAge()+"||"+dateFormat.format(personZhangsan.getCyrzDate())+"||"+personZhangsan.getChilds().size()); }
4.总结:
总结copy 基本数据类型和自定义数据类型都可以进行copy是真正的copye,注意:自定义数据类型并没有真正的进行实现coye,它们的内存地址还是同一个,修改任何一个都会影响其他