好的,我正在构建这个电子表格应用程序,我正在通过命令行界面实现它,其中我有某些命令,例如 exit,它会终止程序。
所以我有这个应用程序类,其中有以下字段:
private ArrayList<Spreadsheet> spreadsheets;
private Spreadsheet worksheet;
这个方法:
public void newSpreadsheet() {
worksheet = new Spreadsheet();
spreadsheets.add(worksheet);
}
然后我有这个 CommandIntepreter 类,它看起来像这样:
package ui;
import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import ui.command.Command;
import ui.command.ExitCommand;
import ui.command.FailedCommand;
import ui.command.PrintCommand;
import ui.command.NewCommand;
import ui.command.ListCommand;
import ui.command.ChangeCommand;
import ui.command.SetCommand;
import ui.command.GetCommand;
import spreadsheet.*;
import spreadsheet.arithmetic.*;
public final class CommandInterpreter {
private CommandInterpreter() {
// The class should not be instanciated.
}
public static Command interpret(final Scanner scanner) {
final String keyword = scanner.next();
switch(keyword) {
case "exit":
return new ExitCommand();
case "pws":
return new PrintCommand();
case "ns":
return new NewCommand();
case "ls":
return new ListCommand();
case "cws":
return new ChangeCommand();
case "set":
return new SetCommand();
case "get":
return new GetCommand();
}
return new FailedCommand(
String.format("Illegal start of command, \"%s\".", keyword));
}
}
然后我创建了如下所示的 NewCommand 类:
package ui.command;
import spreadsheet.Application;
import spreadsheet.Spreadsheet;
public final class NewCommand
extends Command {
public void execute() {
Application.instance.newSpreadsheet();
}
}
当我写 ns 时应该制作一个新的电子表格。但是当我这样做时,什么都没有发生,所以你能告诉我为什么会这样吗?