所謂回調,就是客戶程序C調用服務程序S中的某個函數A,然后S又在某個時候反過來調用C中的某個函數B,對于C來說,這個B便叫做回調函數。
例:
1.class A,class B
2.class A實現接口operate
3.class B擁有一個參數為operate接口類型的函數test(operate o)
4.class A運行時調用class B中test函數,以自身傳入參數
5.class B已取得A,就可以隨時回調A所實現的operate接口中的方法
public interface InterestingEvent
{
// This is just a regular method so it can return something or
// take arguments if you like.
public void interestingEvent ();
}
public class EventNotifier
{
private InterestingEvent ie;
private boolean somethingHappened;
public EventNotifier (InterestingEvent event)
{
// Save the event object for later use.
ie = event;
// Nothing to report yet.
somethingHappened = false;
}
//
public void doWork ()
{
// Check the predicate, which is set elsewhere.
if (somethingHappened)
{
// Signal the even by invoking the interface's method.
ie.interestingEvent ();
}
//
}
//
}
public class CallMe implements InterestingEvent
{
private EventNotifier en;
public CallMe ()
{
// Create the event notifier and pass ourself to it.
en = new EventNotifier (this);
}
// Define the actual handler for the event.
public void interestingEvent ()
{
// Wow! Something really interesting must have occurred!
// Do something
}
//
}