// A function which accepts another function as an argument// (and will automatically invoke that function when it completes - note that there is no explicit call to callbackFunction)funct printANumber(int number, funct callbackFunction) {printout("The number you provided is: " + number);}
// a function which we will use in a driver function as a callback functionfunct printFinishMessage() {printout("I have finished printing numbers.");}
// Driver methodfunct event() {printANumber(6, printFinishMessage);}
如果您调用事件(),则结果:
The number you provided is: 6I have finished printing numbers.
#include<stdio.h>int func(int, int);int main(void){int result1,result2;/* declaring a pointer to a function which takestwo int arguments and returns an integer as result */int (*ptrFunc)(int,int);
/* assigning ptrFunc to func's address */ptrFunc=func;
/* calling func() through explicit dereference */result1 = (*ptrFunc)(10,20);
/* calling func() through implicit dereference */result2 = ptrFunc(10,20);printf("result1 = %d result2 = %d\n",result1,result2);return 0;}
int func(int x, int y){return x+y;}
/* callback.c */#include<stdio.h>#include"reg_callback.h"
/* callback function definition goes here */void my_callback(void){printf("inside my_callback\n");}
int main(void){/* initialize function pointer tomy_callback */callback ptr_my_callback=my_callback;printf("This is a program demonstrating function callback\n");/* register our callback function */register_callback(ptr_my_callback);printf("back inside main program\n");return 0;}
/* reg_callback.h */typedef void (*callback)(void);void register_callback(callback ptr_reg_callback);
/* reg_callback.c */#include<stdio.h>#include"reg_callback.h"
/* registration goes here */void register_callback(callback ptr_reg_callback){printf("inside register_callback\n");/* calling our callback function my_callback */(*ptr_reg_callback)();}
public interface Callback{public void notify(Result result);}
呼叫者或更高级别的类
public Class Caller implements Callback{Callee ce = new Callee(this); //pass self to the callee
//Other functionality//Call the Asynctaskce.doAsynctask();
public void notify(Result result){//Got the result after the callee has finished the task//Can do whatever i want with the result}}
Callee或较低层函数
public Class Callee {Callback cb;Callee(Callback cb){this.cb = cb;}
doAsynctask(){//do the long running task//get the resultcb.notify(result);//after the task is completed, notify the caller}}
public class Activity implements Widget.OnClickEventListener{public Button mButton;public CheckBox mCheckBox;private static Activity mActivityHandler;public static Activity getActivityHandle(){return mActivityHandler;}public Activity (){mActivityHandler = this;mButton = new Button();mButton.setOnClickEventListner(this);mCheckBox = new CheckBox();mCheckBox.setOnClickEventListner(this);}public void onClick (Widget source){if(source == mButton){mButton.setButtonText("Thank you for clicking me...");System.out.println(((Button) mButton).getButtonText());}if(source == mCheckBox){if(mCheckBox.isChecked()==false){mCheckBox.setCheck(true);System.out.println("The checkbox is checked...");}else{mCheckBox.setCheck(false);System.out.println("The checkbox is not checked...");}}}public void doSomeWork(Widget source){source.clickEvent();}}
其他类
public class OtherClass implements Widget.OnClickEventListener{Button mButton;public OtherClass(){mButton = Activity.getActivityHandle().mButton;mButton.setOnClickEventListner(this);//interested in the click event //of the button}@Overridepublic void onClick(Widget source) {if(source == mButton){System.out.println("Other Class has also received the event notification...");}}
主类
public class Main {public static void main(String[] args) {// TODO Auto-generated method stubActivity a = new Activity();OtherClass o = new OtherClass();a.doSomeWork(a.mButton);a.doSomeWork(a.mCheckBox);}}
Multithreaded Method(Some arguments){Do fancy multithreaded stuff....}
Main(){Some stuff I wanna do = some tasksMulthreaded Method(Some stuff I wanna do)}