import java.util.ArrayList; import java.util.List; public class HistoryCommand { private final List commandHistory = new ArrayList<>(); public void addCommand(String command) { commandHistory.add(command); } // 返回副本,避免外部直接修改内部列表 public List getHistory() { return new ArrayList<>(commandHistory); } public void printHistory() { System.out.println("=== 命令历史 ==="); for (int i = 0; i < commandHistory.size(); i++) { System.out.printf("%d: %s%n", i + 1, commandHistory.get(i)); } } public void clearHistory() { commandHistory.clear(); } }