先贴代码有空来写内容。
备忘录1
1 //简单的备忘录,只可以记录上一次修改前的状态,实现撤回一次的操作。 2 class Student{ 3 private String name; 4 private String age; 5 private String gender; 6 //在Student类中直接设立一个Student实例,用于存储该类对象的上一个状态 7 private Student memento=null; 8 //构造函数就不啰嗦了 9 public Student(){} 10 public Student(String n,String a,String g){ 11 name = n; 12 age = a; 13 gender = g; 14 } 15 //在调用可能改变类属性的函数时,要调用saveMemento以备份修改前的实例。 16 public void setName(String n){ 17 saveMemento(); 18 name = n; 19 } 20 public String getName(){ 21 return name; 22 } 23 //和上面一样就不罗嗦了 24 public void setAge(String a){ 25 saveMemento(); 26 age = a; 27 } 28 public String getAge(){ 29 return age; 30 } 31 public void setGender(String g){ 32 saveMemento(); 33 gender = g; 34 } 35 public String getGender(){ 36 return gender; 37 } 38 //new 一个本类实例,用于备份当前实例 39 private void saveMemento(){ 40 memento = new Student(this.name,this.age,this.gender); 41 } 42 //返回备份 43 public Student getMemento(){ 44 return memento; 45 } 46 public String toString(){ 47 return "name: "+name+" age: "+age+" gender: "+gender; 48 } 49 }
测试类
1 public class Test{ 2 public static void main(String[] args){ 3 Student tom = new Student("Tom","16","m"); 4 5 System.out.println("初始信息:"+tom); 6 //调用set函数,tom发生了改变 7 tom.setGender("f"); 8 System.out.println("修改后信息:"+tom); 9 //tom回到改变前的状态 10 tom = tom.getMemento(); 11 System.out.println("撤回到修改前:"+tom); 12 13 14 System.out.println("初始信息:"+tom); 15 tom.setName("Jarry"); 16 System.out.println("修改后信息:"+tom); 17 tom = tom.getMemento(); 18 System.out.println("撤回到修改前:"+tom); 19 } 20 }
备忘录2
1 //这个备忘录可以实现多次撤回。 2 import java.util.Stack; 3 class Student{ 4 private String name; 5 private String age; 6 private String gender; 7 //在Student类中直接设立一个存储Student实例的栈,用于存储该类对象的备份 8 private Stack<Student> memento = null; 9 //构造函数就不啰嗦了 10 public Student(){} 11 public Student(String n,String a,String g){ 12 name = n; 13 age = a; 14 gender = g; 15 memento = new Stack<Student>(); 16 } 17 //在调用可能改变类属性的函数时,要调用saveMemento以备份修改前的实例。 18 public void setName(String n){ 19 saveMemento(); 20 name = n; 21 } 22 public String getName(){ 23 return name; 24 } 25 //和上面一样就不罗嗦了 26 public void setAge(String a){ 27 saveMemento(); 28 age = a; 29 } 30 public String getAge(){ 31 return age; 32 } 33 public void setGender(String g){ 34 saveMemento(); 35 gender = g; 36 } 37 public String getGender(){ 38 return gender; 39 } 40 //将实例的当前状态push到备忘录中 41 private void saveMemento(){ 42 memento.push(new Student(this.name,this.age,this.gender)); 43 } 44 //这里不再返回一个新的Student,而是pop出备忘录中的实例,用它的属性给本this的属性赋值 45 public Student getMemento(){ 46 Student preMemento = memento.pop(); 47 name = preMemento.getName(); 48 age = preMemento.getAge(); 49 gender = preMemento.getGender(); 50 return this; 51 } 52 public String toString(){ 53 return "name: "+name+" age: "+age+" gender: "+gender; 54 } 55 }
测试类
public class Test{ public static void main(String[] args){ Student tom = new Student("Tom","16","m"); System.out.println("初始信息:"+tom); //调用set函数,tom发生了改变 tom.setGender("f"); System.out.println("修改后信息:"+tom); tom.setName("Jarry"); System.out.println("修改后信息:"+tom); tom.setAge("17"); System.out.println("修改后信息:"+tom); tom.setName("haha"); System.out.println("修改后信息:"+tom); //tom回到改变前的状态 System.out.println("撤回到修改前:"+tom.getMemento()); System.out.println("撤回到修改前:"+tom.getMemento()); System.out.println("撤回到修改前:"+tom.getMemento()); System.out.println("撤回到修改前:"+tom.getMemento()); } }
memento3
时间: 2024-10-12 01:19:24