簡體   English   中英

可以從另一個插件調用重寫的方法嗎?

[英]Is possible to call an overridden method from another 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對象,但是如果您希望執行MySocketServer的行為,則需要構造其中之一。

您還應該更改(至少):

MServerSocket.send(clientOutput, input)

...至

super.send(clientOutput, input)

...因為這是從父類委托給方法的正確方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM