import java.util.ArrayList; import java.util.List; public class HistoryCommand { private List commandHistory = new ArrayList<>(); // 添加命令到历史 public void addCommand(String command) { commandHistory.add(command); } // 获取所有历史命令 public List getHistory() { return new ArrayList<>(commandHistory); // 返回副本,保护原数据 } // 打印历史记录 public void printHistory() { for (int i = 0; i < commandHistory.size(); i++) { System.out.println((i + 1) + ". " + commandHistory.get(i)); } } // 清空历史 public void clear() { commandHistory.clear(); } }