public Memento createMemento(){ Memento m = new Memento(menoy); fruits.stream().filter(x -> x.startsWith("好吃")).forEach(x -> { m.addFruits(x); }); return m; }
publicclassTest{ publicstaticvoidmain(String[] args){ Gamer gamer = new Gamer(100); Memento memento = null; ArrayList<String> list = new ArrayList<>(); list.add("好吃的苹果"); list.add("梨"); list.add("好吃的橘子"); list.add("香蕉"); gamer.setFruits(list); System.out.println("我是原本的" + gamer);
Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int i = scanner.nextInt(); if (i == 1) { memento = gamer.createMemento(); } if (i == 2) { gamer.restoreMemento(memento); System.out.println("我是回退的:" + gamer); } } } }