Java 自动从未知类调用方法

Java 自动从未知类调用方法,java,Java,首先,我是Java新手,我对Java还不太了解,我刚刚提出了这个新想法 假设我有一个方法methodConditionString,String,String,我想放在任何类中 代码的场景如下所示: 一切开始的地方 函数可以调用的类 methodConditionString、String、String能够触发的线程 所以我的问题是,MyThread有可能在它注册的任何类中调用methodConditionString,String,String,就像侦听和等待被调用一样 就像我说的,我对Jav

首先,我是Java新手,我对Java还不太了解,我刚刚提出了这个新想法

假设我有一个方法methodConditionString,String,String,我想放在任何类中

代码的场景如下所示:

一切开始的地方

函数可以调用的类

methodConditionString、String、String能够触发的线程

所以我的问题是,MyThread有可能在它注册的任何类中调用methodConditionString,String,String,就像侦听和等待被调用一样

就像我说的,我对Java还不太了解,我不知道这是什么样的函数,或者这是否可能,我只是想到了这个想法。
因此,如果有人能告诉我,解释或给我一个链接,以供参考,我将非常感谢。我也愿意接受任何澄清。谢谢大家!

若要从任何类调用methodCondition,必须像静态方法一样声明。可以在不实例化容器类的情况下调用statics方法

public static void methodCondition(String data1, String data2, String data3){
    //Do something about the data when this method is fire from Thread
}
声明like static后,您可以直接调用它:

MainClass.methodCondition(...);

所有类都必须在同一个包中,或者导入要使用methodCondition的MainClass。

首先,如果有人认为这个问题都是关于多线程的,我想说声抱歉。因为像我这样的初学者提出的问题不清楚,所以即使我投了反对票也没关系。这个问题是我想要的,但我还不知道它是什么,因为我还不太了解Java

第二,谢谢你的回答。我真的很感激,即使答案不是我想要的,我重申这是我的错,因为问题不清楚

第三,我在另一个论坛上问了很多问题,只是想知道我想要什么

经过很多问题,我很高兴找到它,它被称为。要明确的是,我真正想要的是从未知类中找到方法的特定名称,若它存在,那个么调用它来触发特定任务

此外,我所做的代码如下所示,它是我的工作:

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.

现在我知道了-

如果您不知道类名,最好将其放在接口中,接受该接口作为线程的输入,并从接口引用调用它。此方法可以是线程内部的,也可以是普通接口。下面是具有内部接口的示例

线程代码:

其他类别例如:


使用像Swing一样的监听系统。听起来像是观察者模式,在网上有很多东西可以找到。我想人们应该回顾一下这个系统的目的。
public static void methodCondition(String data1, String data2, String data3){
    //Do something about the data when this method is fire from Thread
}
MainClass.methodCondition(...);
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.
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
    }
}