我正在为一个程序编写两个插件,其中一个是“库插件”,包含许多其他插件使用的类,另一个是基于这个库的插件。除了一件事之外,所有的工作都很好。在我的库插件中,我编写了一个中文化的socket类:
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方法所述。
所以我在这里,有人能给我一个答案和可能的解决方案吗?提前谢谢。
发布于 2017-06-06 22:01:45
您发布的代码中存在错误:
MServerSocket.send(clientOutput, input);这是不正确的,因为send不是static方法。它应该写成这样:
this.send(clientOutput, input);或
super.send(clientOutput, input);或者简单地说
send(clientOutput, input);但是为了回答您的问题,您看到"print“的原因是您在一个是MServerSocket实例而不是MyServerSocket实例的实例上调用了initServer方法。因为它是一个MServerSocket实例,所以没有覆盖方法。
https://stackoverflow.com/questions/44390912
复制相似问题