简体   繁体   中英

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:

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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