先贴代码有空来写内容。
备忘录1
//简单的备忘录,只可以记录上一次修改前的状态,实现撤回一次的操作。
class Student{
private String name;
private String age;
private String gender;
//在Student类中直接设立一个Student实例,用于存储该类对象的上一个状态
private Student memento=null;
//构造函数就不啰嗦了
public Student(){}
public Student(String n,String a,String g){
name = n;
age = a;
gender = g;
}
//在调用可能改变类属性的函数时,要调用saveMemento以备份修改前的实例。
public void setName(String n){
saveMemento();
name = n;
}
public String getName(){
return name;
}
//和上面一样就不罗嗦了
public void setAge(String a){
saveMemento();
age = a;
}
public String getAge(){
return age;
}
public void setGender(String g){
saveMemento();
gender = g;
}
public String getGender(){
return gender;
}
//new 一个本类实例,用于备份当前实例
private void saveMemento(){
memento = new Student(this.name,this.age,this.gender);
}
//返回备份
public Student getMemento(){
return memento;
}
public String toString(){
return "name: "+name+" age: "+age+" gender: "+gender;
}
}
测试类
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回到改变前的状态
tom = tom.getMemento();
System.out.println("撤回到修改前:"+tom); System.out.println("初始信息:"+tom);
tom.setName("Jarry");
System.out.println("修改后信息:"+tom);
tom = tom.getMemento();
System.out.println("撤回到修改前:"+tom);
}
}
备忘录2
//这个备忘录可以实现多次撤回。
import java.util.Stack;
class Student{
private String name;
private String age;
private String gender;
//在Student类中直接设立一个存储Student实例的栈,用于存储该类对象的备份
private Stack<Student> memento = null;
//构造函数就不啰嗦了
public Student(){}
public Student(String n,String a,String g){
name = n;
age = a;
gender = g;
memento = new Stack<Student>();
}
//在调用可能改变类属性的函数时,要调用saveMemento以备份修改前的实例。
public void setName(String n){
saveMemento();
name = n;
}
public String getName(){
return name;
}
//和上面一样就不罗嗦了
public void setAge(String a){
saveMemento();
age = a;
}
public String getAge(){
return age;
}
public void setGender(String g){
saveMemento();
gender = g;
}
public String getGender(){
return gender;
}
//将实例的当前状态push到备忘录中
private void saveMemento(){
memento.push(new Student(this.name,this.age,this.gender));
}
//这里不再返回一个新的Student,而是pop出备忘录中的实例,用它的属性给本this的属性赋值
public Student getMemento(){
Student preMemento = memento.pop();
name = preMemento.getName();
age = preMemento.getAge();
gender = preMemento.getGender();
return this;
}
public String toString(){
return "name: "+name+" age: "+age+" gender: "+gender;
}
}
测试类
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