标签:mic stat 计算机 tst 图片 ide this pre bsp
package designpattern.memento; public class Student { private String name;// 姓名 private String major;// 专业 private String studentNo;// 学号 public Student(String name, String major, String studentNo) { super(); this.name = name; this.major = major; this.studentNo = studentNo; } public Memento createMemento() { return new Memento(this.name, this.major); } public void restoreMemento(Memento memento) { this.name = memento.getName(); this.major = memento.getMajor(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } public String getStudentNo() { return studentNo; } public void setStudentNo(String studentNo) { this.studentNo = studentNo; } @Override public String toString() { return "Student [name=" + name + ", major=" + major + ", studentNo=" + studentNo + "]"; } }
package designpattern.memento; public class Memento { private String name; private String major; public Memento(String name, String major) { super(); this.name = name; this.major = major; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } }
package designpattern.memento; public class CareTaker { private Memento memento; public Memento getMemento() { return memento; } public void setMemento(Memento memento) { this.memento = memento; } }
package designpattern.memento; public class Client { public static void main(String[] args) { Student student = new Student("郝鑫仁", "计算机", "1234"); CareTaker careTaker = new CareTaker(); careTaker.setMemento(student.createMemento()); System.out.println(student); System.out.println("开始点名..."); // 伪装成其他同学 student.setName("齐步莱"); student.setMajor("自动化"); System.out.println(student); System.out.println("点名结束"); // 变回自己 student.restoreMemento(careTaker.getMemento()); System.out.println(student); } }
Student [name=郝鑫仁, major=计算机, studentNo=1234] 开始点名... Student [name=齐步莱, major=自动化, studentNo=1234] 点名结束 Student [name=郝鑫仁, major=计算机, studentNo=1234]
标签:mic stat 计算机 tst 图片 ide this pre bsp
原文地址:https://www.cnblogs.com/imoqian/p/10970138.html