簡體   English   中英

自動調用未知類的方法

[英]Automatically call a method from unknown class

首先,我是 Java 新手,對此我還不太了解,我只是想出了這個新想法。

假設我有一個方法methodCondition(String,String,String)我想放在任何類中。

代碼場景如下:

一切開始的地方

public class MainClass{

    public static void main(String... args) 
    {
        //Whe everything started, call StartFunction from proccesshelper class to Start a Thread.
        ProccessHelper phelper = new ProccessHelper();
        phelper.StartFunction();
    }

    public void methodCondition(String data1, String data2, String data3){
        //Do something about the data when this method is fire from Thread
    }
}

函數可以調用的類

public class ProccessHelper{

    //Some function here

    public void StartFunction(){

        MyThread mythread = new MyThread();
        Thread t = new Thread(mythread);
        t.start();
    }

    //Some function here
}

一個可以methodCondition(String,String,String)線程

public class MyThread implements Runnable {

    volatile boolean StopThread = false;
    public MyThread(){}

    public void Stop(boolean stopThread){
        this.StopThread = stopThread;
    }

    public void run(){
        if(dontLoop){
            while(true){
                if(condition = true){
                    /*
                    * if the condition here is true then call "eventMethod" from any unkown class.
                    */
                    methodCondition(String data1, String data2, String data3);
                }
            }
        }
    }
}

所以我的問題是, MyThread有可能在任何注冊的類中調用methodCondition(String,String,String)就像監聽和等待調用一樣?

就像我說的,我對Java的了解還不是很多,我不知道這是什么功能,或者如果可能的話我只是想出了這個想法。 因此,如果有人能告訴、解釋或提供有關我正在努力實現的任何參考的鏈接,將不勝感激。 我也願意接受任何澄清。 謝謝!

如果要從任何類調用 methodCondition,則必須像靜態方法一樣聲明。 可以在不實例化容器類的情況下調用靜態方法。

public static void methodCondition(String data1, String data2, String data3){
    //Do something about the data when this method is fire from Thread
}

像 static 一樣聲明后,您可以直接調用它:

MainClass.methodCondition(...);

所有類必須在同一個包中,或者在要使用 methodCondition 的地方導入 MainClass。

首先,如果有人認為這個問題與multithreading有關,我想說聲抱歉。 由於像我這樣的beginner提出的不清楚的問題,即使我拒絕投票也沒關系。 這個問題是我想要的,但我還不知道它是什么,因為我對 Java 還不太了解。

其次感謝@GhostCat的回答。 我真的很感激即使答案不是我想要的,我再說一遍,由於問題不清楚,這是我的錯。

第三,我在不同的forum做了很多問題,只是為了找出我想要的。

經過很多問題,我很高興找到它,它被稱為Invoking Methods 需要明確的是,我真正想要的是從未知類中查找特定method名稱,如果存在,則調用它來觸發特定任務。

此外,我所做的代碼如下,它的工作原理:

Class c=Class.forName("MainActivity"); 
Method m=c.getMethod("methodCondition", String.class, String.class, String.class); //The method has 3 String paramaters so I have to intialize it otherwise it will produce an error that the method was not found.
Object t = c.newInstance();
m.invoke(t,"Hello Word!", "this is", "to Invoke Method"); //Now invoke the method with the value or paramaters.

現在我知道了。 :-)

如果您不知道類名,最好將它放在一個interface並接受該接口作為您線程的輸入並從接口引用中調用它。 此方法可以是線程內部的,也可以是普通接口。 下面是帶有內部接口的示例。

線程代碼:

class MyThread implements Runnable {

    interface interfaceName {
        void methodName(String data1, String data2, String data3);
    }

    interfaceName interfaceReference = null;

    // Other members declaration

    private MyThread(interfaceName obj) {
        interfaceReference = obj;
    }

    public static MyThread getInstance(interfaceName obj) {
        if (obj == null) {
            throw new NullPointerException();
        }
        return new MyThread(obj);
    }

    public void run() {
        // Do your stuff
        interfaceReference.methodName("", "", "");
        // Do your stuff
    }
}

其他類示例:

public class Temp implements MyThread.interfaceName {

    public static void main(String[] args) {
        Temp t = new Temp();
        MyThread mt = MyThread.getInstance(t);
    }

    public void methodName(String data1, String data2, String data3) {
        // Do your stuff
    }
}

暫無
暫無

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

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