先把spring那5个核心的jar包导入到项目中
spring-core-3.2.0.RELEASE.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
新建一个Person类
public class Person {
private Double avgScore;
private String courseName;
private int num;
private Date createTime;
public Double getAvgScore() {
return avgScore;
}
public void setAvgScore(Double avgScore) {
this.avgScore = avgScore;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
@Override
public String toString() {
return "Person [avgScore=" + avgScore + ", courseName=" + courseName
+ ", num=" + num + ", createTime=" + createTime + "]";
}
}
新建一个Student类
public class Student extends Person{
private String studentName;
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
@Override
public String toString() {
return "Student [studentName=" + studentName + ", getAvgScore()="
+ getAvgScore() + ", getCourseName()=" + getCourseName()
+ ", getNum()=" + getNum() + ", getCreateTime()="
+ getCreateTime() + ", toString()=" + super.toString()
+ ", getClass()=" + getClass() + ", hashCode()=" + hashCode()
+ "]";
}
}
编写一个测试类
public class Test {
@Test
public void test(){
Person p=new Person();
p.setAvgScore(3.0);
p.setCourseName("courseName");
p.setCreateTime(new Date());
p.setNum(2);
Student s=new Student();
s.setStudentName("studentName");
BeanUtils.copyProperties(p, s);
System.out.println(p);
System.out.println("-------------------");
System.out.println(s);
}
}
运行结果为
Person [avgScore=3.0, courseName=courseName, num=2, createTime=Wed Aug 30 08:41:56 CST 2017]
-------------------
Student [studentName=studentName, getAvgScore()=3.0, getCourseName()=courseName, getNum()=2, getCreateTime()=Wed Aug 30 08:41:56 CST 2017, toString()=Person [avgScore=3.0, courseName=courseName, num=2, createTime=Wed Aug 30 08:41:56 CST 2017], getClass()=class cn.eaglezsx.test.Student, hashCode()=5784348]
可以看到Person对象中的属性值复制到了Student中
需求:将一个list中对象的属性值复制到另一个list中
@Test
public void t2(){
List pL=new ArrayList<>();
Person p=new Person();
p.setAvgScore(3.0);
p.setCourseName("courseName");
p.setCreateTime(new Date());
p.setNum(2);
Person p2=new Person();
p2.setAvgScore(4.0);
pL.add(p);
pL.add(p2);
List sL=new ArrayList<>();
for(Person person:pL){
Student student=new Student();
BeanUtils.copyProperties(person, student);
sL.add(student);
}
System.out.println(pL);
System.out.println("-----------------------");
System.out.println(sL);
}
运行结果为
[Person [avgScore=3.0, courseName=courseName, num=2, createTime=Wed Aug 30 08:48:49 CST 2017], Person [avgScore=4.0, courseName=null, num=0, createTime=null]]
-----------------------
[Student [studentName=null, getAvgScore()=3.0, getCourseName()=courseName, getNum()=2, getCreateTime()=Wed Aug 30 08:48:49 CST 2017, toString()=Person [avgScore=3.0, courseName=courseName, num=2, createTime=Wed Aug 30 08:48:49 CST 2017], getClass()=class cn.eaglezsx.test.Student, hashCode()=32846040], Student [studentName=null, getAvgScore()=4.0, getCourseName()=null, getNum()=0, getCreateTime()=null, toString()=Person [avgScore=4.0, courseName=null, num=0, createTime=null], getClass()=class cn.eaglezsx.test.Student, hashCode()=5890565]]
可以看到pL总对象的值复制到了sL中了。
转自: https://blog.csdn.net/zsx157326/article/details/77693220