繁体   English   中英

可以从另一个插件调用重写的方法吗? [用于Minecraft插件]

[英]Is possible to call an overridden method from another plugin? [Is for a Minecraft plugin]

我正在为程序编写两个插件,一个是“库插件”,其中包含许多其他插件使用的类,另一个是基于该库的一个插件。 除一件事外,其他所有工具都运作良好。 在我的库插件中,我编写了一个套接字类:

public class MServerSocket {
    public void initServer(int port) {
        //Code to receive message from client
        execute(input, clientOutput);
    }

    public void execute(String input, DataOutputStream clientOutput) {
        System.out.println(input);
        send(clientOutput, input);
    }

    public void send(DataOutputStream clientOutput, String output) {
        //Code to send message to client
    }
}

在另一个插件中,我扩展了此类并重写了execute方法以执行某些操作,如下所示:

public class MySocketServer extends MServerSocket {
    @Override
    public void execute(String input, DataOutputStream clientOutput) {
        //Do something
        MServerSocket.send(clientOutput, input)
    }
}

现在,我的第二个插件应该覆盖我的库插件类,但是没有。 在第二个插件中,我这样称呼它:

public class Main {
    public void onEnable() { //method called to load plugin
        private static MServerSocket socket = new MServerSocket();
        socket.initServer(12980);
    }
}

当我向套接字发送套接字消息时,它将按照库execute方法中的说明打印到控制台。

所以我来了,有人可以给我答案,可能还可以解决方案吗? 提前致谢。

您发布的代码中有错误:

  MServerSocket.send(clientOutput, input);

这是不正确的,因为send不是static方法。 应该这样写:

 this.send(clientOutput, input);

要么

 super.send(clientOutput, input);

或简单地

 send(clientOutput, input);

但是要回答您的问题,您看到“打印”的原因是,您正在作为MServerSocket实例而不是MyServerSocket实例的实例上调用initServer方法。 由于它是MServerSocket实例, 因此它没有重写方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM