package com.java.test.clone; /** * @author Spark * @since 2010-02-10 * */ public class UserA implements Cloneable { private String name; private int age; @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } package com.java.test.clone; /** * @author Spark * @since 2010-02-10 * */ public class UserB { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } package com.java.test.clone; import java.lang.reflect.InvocationTargetException; import org.apache.commons.beanutils.BeanUtils; /** * @author Spark * @since 2010-02-10 * */ public class MainTest { /** * @param args * @throws CloneNotSupportedException * @throws InvocationTargetException * @throws IllegalAccessException */ public static void main(String[] args) throws CloneNotSupportedException, IllegalAccessException, InvocationTargetException { UserA a = new UserA(); a.setName("Tom"); a.setAge(18); //1.properties clone UserA 水货 = new UserA(); 水货 = (UserA)a.clone(); System.out.println(水货.getName()+" " + 水货.getAge()); //2.properties copy UserB b = new UserB(); BeanUtils.copyProperties(b,a); System.out.println(b.getName()+" " + b.getAge()); } }
Result:
Tom 18 Tom 18