package xiao; public class User2 { private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public User2(String name, String password) { super(); this.name = name; this.password = password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User2 [name=" + name + ", password=" + password + "]"; } }
package xiao; public class User1 { private String name; private String password; private String sex; public String getName() { return name; } public User1() { super(); // TODO Auto-generated constructor stub } public User1(String name, String password, String sex) { super(); this.name = name; this.password = password; this.sex = sex; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "User1 [name=" + name + ", password=" + password + ", sex=" + sex + "]"; } }
package xiao; import java.lang.reflect.InvocationTargetException; import org.apache.commons.beanutils.BeanUtils; public class Test { public static void main(String[] args) throws IllegalAccessException, InvocationTargetException { User1 user1 = new User1(); User2 user2 = new User2("xiao", "xiong"); BeanUtils.copyProperties(user1, user2); System.out.println(user1); } }
User1 [name=xiao, password=xiong, sex=null]
依赖Jar包:
commons-beanutils-1.8.3.jar
commons-logging-1.1.1.jar
时间: 2024-11-05 02:20:52