//游戏存进度
/*class GameRole{
private int vit;
public int getVit() {
return vit;
}
public void setVit(int vit) {
this.vit = vit;
}
private int atk;
public int getAtk() {
return atk;
}
public void setAtk(int atk) {
this.atk = atk;
}
private int def;
public int getDef() {
return def;
}
public void setDef(int def) {
this.def = def;
}
public void StateDisplay() {
System.out.println("当前角色状态:");
System.out.println("体力:"+this.getVit());
System.out.println("攻击力:"+this.getAtk());
System.out.println("防御力:"+this.getDef());
}
public void GetInitState() {
this.vit = 100;
this.atk = 100;
this.def = 100;
}
public void Fight() {
this.vit = 0;
this.atk = 0;
this.def = 0;
}
}
public class Main {
public static void main(String args[])
{
GameRole g = new GameRole();
g.GetInitState();
g.StateDisplay();
GameRole g2 = new GameRole();
int a = g.getVit();
int b = g.getAtk();
int c = g.getDef();
g2.setVit(a);
g2.setAtk(b);
g2.setDef(c);
g.Fight();
g.StateDisplay();
g=g2;
g.StateDisplay();
}
}*/
//备忘录模式
/*class Originator{
private String state;
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Memento CreateMemento() {
return (new Memento(state));
}
public void SetMemento(Memento memento) {
state = memento.getState();
}
public void Show() {
System.out.println("State=" + state);
}
}
class Memento{
private String state;
public Memento(String state) {
super();
this.state = state;
}
public String getState() {
return state;
}
}
class Caretaker{
private Memento memento;
public Memento getMemento() {
return memento;
}
public void setMemento(Memento memento) {
this.memento = memento;
}
}
public class Main {
public static void main(String args[])
{
Originator o = new Originator();
o.setState("On");
o.Show();
Caretaker c = new Caretaker();
Memento m = new Memento(o.getState());
m = o.CreateMemento();
c.setMemento(m);
o.setState("Off");
o.Show();
o.SetMemento(c.getMemento());
o.Show();
}
}*/
//游戏进度备忘
class GameRole{
private int vit;
public int getVit() {
return vit;
}
public void setVit(int vit) {
this.vit = vit;
}
private int atk;
public int getAtk() {
return atk;
}
public void setAtk(int atk) {
this.atk = atk;
}
private int def;
public int getDef() {
return def;
}
public void setDef(int def) {
this.def = def;
}
public void StateDisplay() {
System.out.println("当前角色状态:");
System.out.println("体力:"+this.getVit());
System.out.println("攻击力:"+this.getAtk());
System.out.println("防御力:"+this.getDef());
}
public RoleStateMemento SaveState() {
return (new RoleStateMemento(vit,atk,def));
}
public void RecoveryState(RoleStateMemento memento) {
this.vit = memento.getVit();
this.atk = memento.getAtk();
this.def = memento.getDef();
}
public void GetInitState() {
this.vit = 100;
this.atk = 100;
this.def = 100;
}
public void Fight() {
this.vit = 0;
this.atk = 0;
this.def = 0;
}
}
class RoleStateMemento{
private int vit;
public int getVit() {
return vit;
}
public void setVit(int vit) {
this.vit = vit;
}
private int atk;
public int getAtk() {
return atk;
}
public void setAtk(int atk) {
this.atk = atk;
}
private int def;
public int getDef() {
return def;
}
public void setDef(int def) {
this.def = def;
}
public RoleStateMemento(int vit, int atk, int def) {
super();
this.vit = vit;
this.atk = atk;
this.def = def;
}
}
class RoleStateCaretaker{
private RoleStateMemento memento;
public RoleStateMemento getMemento() {
return memento;
}
public void setMemento(RoleStateMemento memento) {
this.memento = memento;
}
}
public class Main {
public static void main(String args[])
{
GameRole g = new GameRole();
g.GetInitState();
g.StateDisplay();
RoleStateCaretaker r = new RoleStateCaretaker();
RoleStateMemento r1 = new RoleStateMemento(g.getVit(),g.getAtk(),g.getDef());
r.setMemento(r1);
g.Fight();
g.StateDisplay();
g.RecoveryState(r.getMemento());
g.StateDisplay();
}
}
备忘录模式 一步步实现
最新推荐文章于 2022-04-23 21:23:17 发布