2016-10-02 4 views
-2

まず、私は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 
    } 
} 

に機能がmethodCondition(String,String,String)ができているスレッド

public class ProccessHelper{ 

    //Some function here 

    public void StartFunction(){ 

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

    //Some function here 
} 

を呼び出すことができるクラスを開始した

発砲する

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でまだ多くのことを知らないが、どのような機能がこれなのか分からない、あるいはこれが可能ならば、私はこのアイデアを思いついた。 だから、誰かが私が達成しようとしていることについての言及については、誰かが説明したり、説明したり、リンクを張ったりすることができれば、とても感謝しています。私はまた、明確化のために開いています。ありがとうございました!

+0

は、Swingが行うようリスナーシステムを使用してください。 – immibis

+0

はObserverパターンのようなものをね、ネットで見つけることがたくさんがあります。 [StackOverflowの](http://stackoverflow.com/)で – Turo

+1

人々は私が推測する[投票ダウン](http://stackoverflow.com/help/privileges/vote-down)の目的を確認してください。 – lopi

答えて

0

まず、この質問がすべてmultithreadingであると誰かが考えていると申し訳ありません。私のようなbeginnerからの不明確な質問のために私は投票に落ちてもOKです。問題は私が欲しいものですが、まだJavaについてまだあまり知らなかったので、それが何であるか分かりません。

回答は@GhostCatです。私が本当に感謝しているのは、私が欲しいものではないとしても答えが繰り返されます。それは不明確な質問のせいで私のせいです。

第3に、私は別のもので多くの質問をしました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. 

は、今私は知っています。:-)

0

どのクラスからでもmethodConditionを呼び出す場合は、静的メソッドのように宣言する必要があります。静的メソッドは、コンテナクラスをインスタンス化せずに呼び出すことができます。静的のような宣言の後

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

あなたはそれを直接呼び出すことができます。

MainClass.methodCondition(...); 

をすべてのクラスは、あなたがmethodConditionを使用する同じパッケージ、またはインポートMainClassでなければなりません。

0

クラス名がわからない場合は、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 
    } 
} 
関連する問題