publicStudentBO(){ students = new ArrayList<StudentVO>(); StudentVO student1 = new StudentVO("Robert",0); StudentVO student2 = new StudentVO("John",1); students.add(student1); students.add(student2); } publicvoiddeleteStudent(StudentVO student){ students.remove(student.getRollNo()); System.out.println("Student: Roll No " + student.getRollNo() +", deleted from database"); }
//从数据库中检索学生名单 public List<StudentVO> getAllStudents(){ return students; }
public StudentVO getStudent(int rollNo){ return students.get(rollNo); }
publicvoidupdateStudent(StudentVO student){ students.get(student.getRollNo()).setName(student.getName()); System.out.println("Student: Roll No " + student.getRollNo() +", updated in the database"); } }
$ javac -d . src/main/cn/twle/gof/TransferObjectPatternDemo.java $ java cn.twle.gof.TransferObjectPatternDemo Student: [RollNo : 0, Name : Robert ] Student: [RollNo : 1, Name : John ] Student: Roll No 0, updated in the database Student: [RollNo : 0, Name : Michael ]