使用循环根据找到的字符串调用不同的方法

问题描述

我正在使用 Pircbotx (https://github.com/pircbotx/pircbotx/wiki/Documentation) 提供的侦听器来检测何时在 Twitch 聊天中找到命令,并且我正在尝试根据调用的命令使用不同的方法(格式为 !command) .使用的类:监听器、命令。

命令存储在一个由一个字符串(名称)组成的 Command 对象数组中。每个 Command 对象最终都将使用自己的方法,该方法将在 Command 类中定义。 Listeners 对象在实例化时会立即将数组的每个元素放入哈希表(命令)中。

当侦听器检测到消息时,它会使用本地字符串变量 (msg) 进行存储。发生这种情况时,循环遍历 Command 对象数组,然后……应该调用对应于该特定对象的方法,在本例中为 Command.addDeath()。这就是我被卡住的地方。

我以前为我的听众使用了一堆 if 语句,但是当有一堆命令时,事情会变得非常非常混乱。如果我的代码块中的格式很奇怪,请提前道歉,我对使用 Stackverflow 还很陌生,而且我也是一名 Java 新手,正在学习。再次查看代码后,似乎我真的不需要哈希表 - 但我将它留在那里,以防你们对如何处理它们有更好的想法。

public class Listeners {

String name;
String message;    
private static MessageEvent event;
Command [] commandNames = {new Command("!clearchat",new Command("!addDeath")}; 
Hashtable<String,Command> commands = new Hashtable<String,Command>();

 public Listeners() {
    for (int i = 0; i < commandNames.length; i++) {
        commands.put(commandNames[i].name,new Command(commandNames[i].name));
    }   
    
    if (event.getMessage() != null) {
        String msg = event.getMessage();
        for (int x = 0; x < commandNames.length; x++ ) {
            if (msg.startsWith(commandNames[x].name)) {
                // call Command method here 
            }
        }
    }   
}

这是命令类:

public class Command {

String name;

public Command(String name) {
        this.name = name;
}

public static void addDeath() {
    DeathCounter.addDeath();
    Listeners.sendMessage("Death Counter: " + DeathCounter.getDeaths());
} 
}

解决方法

您可以为您的命令使用接口:

public interface Command {
    public abstract void execute();
}

然后让你的命令实现接口:

public class DeathCommand implements Command {
    @Override
    public void execute() {
        DeathCounter.addDeath();
        Listeners.sendMessage("Death Counter: " + DeathCounter.getDeaths());
    }
}

在您的侦听器类中,将命令字符串映射到相应命令的实例:

public class Listeners {

    String name;
    String message;
    private static MessageEvent event;
    static Map<String,Command> commands = new HashMap<>();

    static {
        commands.put("!addDeath",new DeathCommand());
    }

    public Listeners() {
        if (event.getMessage() != null) {
            String msg = event.getMessage();
            Optional<String> key = commands.keySet().stream().filter(k -> msg.startsWith(k)).findFirst();
            key.ifPresent(s -> commands.get(s).execute());
        }
    }
}

我用 HashMap 替换了你的 Hashtable,它在大多数方面都更好(但使用方式相同)。

我有点怀疑将地图作为静态成员,但由于我不熟悉您的用例,所以我保留原样。

编辑

所有 Java 类都有一个默认构造函数(没有参数),除非您编写自己的构造函数(如果您仍然需要,则必须自己编写默认构造函数)。接口没有构造函数,因为它们不能直接实例化。它们只是指定实现类必须具有的方法。这允许您拥有接口类型的引用(命名字段/变量),并且能够在不知道或不必知道它是哪个实现的情况下调用方法。

示例:

Command com = new DeathCommand();
com.execute(); // <- this will run the execute() code in the DeathCommand class
Command com2 = new SomeOtherCommand();
com2.execute(); // <- this will run the execute() code in the SomeOtherCommand class

以上Command的代码就完成了。没有更多了。至于 DeathCommand 和其他实现,您需要添加所需的代码。

每个类和接口都在它自己的名为类型的文件中:

Command.java
DeathCommand.java

关于 HashTable 与 HashMap。我应该说最好使用 Map 和它的实现。如果您需要线程安全,请使用 agilob 指出的 ConcurrentHashMap,因为常规 HashMap 不是线程安全的。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...