需要帮助为java中的命令行接口构建命令类吗
好的,我正在构建这个电子表格应用程序,我正在通过一个命令行界面实现它,在这里我有一些命令,例如exit,它终止程序 我有一个应用程序类,其中有以下字段:需要帮助为java中的命令行接口构建命令类吗,java,command,command-line-interface,Java,Command,Command Line Interface,好的,我正在构建这个电子表格应用程序,我正在通过一个命令行界面实现它,在这里我有一些命令,例如exit,它终止程序 我有一个应用程序类,其中有以下字段: private ArrayList<Spreadsheet> spreadsheets; private Spreadsheet worksheet; 然后我有一个CommandIntepreter类,它看起来像这样: package ui; import java.util.Scanner; import java.util.
private ArrayList<Spreadsheet> spreadsheets;
private Spreadsheet 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));
}
}
package ui.command;
import spreadsheet.Application;
import spreadsheet.Spreadsheet;
public final class NewCommand
extends Command {
public void execute() {
Application.instance.newSpreadsheet();
}
}
然后我创建了NewCommand类,如下所示:
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));
}
}
package ui.command;
import spreadsheet.Application;
import spreadsheet.Spreadsheet;
public final class NewCommand
extends Command {
public void execute() {
Application.instance.newSpreadsheet();
}
}
当我写ns的时候,我应该制作一个新的电子表格。但是当我这样做的时候,什么都没有发生,所以你能告诉我为什么吗?为了创建新的电子表格,你必须调用NewCommand类execute方法。我在你的代码里没有看到你那样做的地方 在此之前,我相信您正在尝试在这个应用程序中使用命令模式。我建议您创建一个名为“Command”的接口,并使用一个方法“execute()”,然后在所有类中实现该命令接口。如果您发现“ns”作为命令行输入,只需将新命令的实例创建为
case "ns":
Command nsCommand = new NewCommand();
nsCOmmand.execute();
return "SOME_MESSAGE"
您的问题描述可能太模糊,任何人都无法真正说出问题所在。A将为您提供简洁的答案。您是否保存了电子表格?我是否可以请您给我一个如何做到这一点的示例?:)