简体   繁体   English

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

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

I'm coding two plugins for a program, one of this plugin is a "library plugin" and contains a lot of classes used by the other plugins, the other is one of the plugin based on this library. 我正在为程序编写两个插件,一个是“库插件”,其中包含许多其他插件使用的类,另一个是基于该库的一个插件。 All works well except one thing. 除一件事外,其他所有工具都运作良好。 In my library plugin I wrote a socket class sintetized to this: 在我的库插件中,我编写了一个套接字类:

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
    }
}

In the other plugin I extend this class and override the execute method to do something, like this: 在另一个插件中,我扩展了此类并重写了execute方法以执行某些操作,如下所示:

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

Now my second plugin should override my library plugin class but it doesn't. 现在,我的第二个插件应该覆盖我的库插件类,但是没有。 In the second plugin I call it in the main like this: 在第二个插件中,我这样称呼它:

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

When I send a socket message to my socket it is printed to the console as said in the library execute method. 当我向套接字发送套接字消息时,它将按照库execute方法中的说明打印到控制台。

So here I am, can someone give me an answer and possibly a solution? 所以我来了,有人可以给我答案,可能还可以解决方案吗? Thanks in advance 提前致谢

Your code currently constructs a MServerSocket object but if you want the behaviour of your MySocketServer to be executed, you need to construct one of those. 您的代码当前正在构造一个MServerSocket对象,但是如果您希望执行MySocketServer的行为,则需要构造其中之一。

You should also change (at a minimum): 您还应该更改(至少):

MServerSocket.send(clientOutput, input)

...to ...至

super.send(clientOutput, input)

...as that's the proper way to delegate to a method from the parent class. ...因为这是从父类委托给方法的正确方法。

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

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