1. 浠涔堟槸鍥炶皟鍑芥暟锛

鍥炶皟鍑芥暟锛屽厜鍚悕瀛楀氨姣旀櫘閫氬嚱鏁拌楂樺ぇ涓婁竴浜涳紝閭e埌搴曚粈涔堟槸鍥炶皟鍑芥暟鍛紵鎭曟垜璇诲緱涔﹀皯锛屾病鏈夊湪閭f湰涔︿笂鐪嬪埌鍏充簬鍥炶皟鍑芥暟鐨勫畾涔夈快三投注网平台—官方网址22270.COM鎴戝湪鐧惧害涓婃悳浜嗕竴涓嬶紝鍙戠幇浼楄绾风涵锛屾湁寰堝ぇ涓閮ㄥ垎閮芥槸浣跨敤绫讳技杩欎箞涓涓満鏅潵璇存槑锛欰鍚涘幓B鍚涘簵閲屼拱涓滆タ锛屾伆濂界己璐э紝A鍚涚暀涓嬪彿鐮佺粰B鍚涳紝鏈夎揣鏃堕氱煡A鍚涖傛劅瑙夎繖涓浜烘洿瀹规槗鎯冲埌鐨勬槸寮傛鎿嶄綔锛岃屼笉鏄洖璋冦傚彟澶栬繕鏈変袱鍙ヨ嫳鏂囪鎴戝嵃璞℃繁鍒伙細1) If you call me, I will call you back; 2) Don't call me, I will call you. 鐪嬭捣鏉ュソ鍍忓緢鏈夐亾鐞嗭紝浣嗘槸浠旂粏涓鎯筹紝鏅氬嚱鏁颁笉涔熷彲浠ュ仛鍒拌繖涓ょ偣鍚楋紵鎵浠ワ紝鎴戣寰楄繖鏍风殑璇存硶閮戒笉鏄緢濡ュ綋锛屽洜涓烘垜瑙夊緱杩欎簺璇存硶閮芥病鏈夋妸鍥炶皟鍑芥暟鐨勭壒鐐硅〃杈惧嚭鏉ワ紝涔熷氨鏄兘鐪嬩笉鍒板拰鏅氬嚱鏁板埌搴曟湁浠涔堝樊鍒備笉杩囷紝鐧惧害鐧剧鐨勮В鏋愭垜瑙夊緱杩樼畻涓嶉敊锛堣櫧鐒剁粡甯稿悙妲界櫨搴︽悳绱...锛夛細鍥炶皟鍑芥暟灏辨槸涓涓氳繃鍑芥暟鎸囬拡璋冪敤鐨勫嚱鏁般傚鏋滀綘鎶婂嚱鏁扮殑鎸囬拡锛堝湴鍧锛変綔涓哄弬鏁颁紶閫掔粰鍙︿竴涓嚱鏁帮紝褰撹繖涓寚閽堣鐢ㄦ潵璋冪敤鍏舵墍鎸囧悜鐨勫嚱鏁版椂锛屾垜浠氨璇磋繖鏄洖璋冨嚱鏁般

涓嬮潰鍏堣璇存垜鐨勭湅娉曘傛垜浠彲浠ュ厛鍦ㄥ瓧闈笂鍏堝仛涓垎瑙o紝瀵逛簬"鍥炶皟鍑芥暟"锛屼腑鏂囧叾瀹炲彲浠ョ悊瑙d负杩欎箞涓ょ鎰忔濓細1) 琚洖璋冪殑鍑芥暟锛2) 鍥炲ご鎵ц璋冪敤鍔ㄤ綔鐨勫嚱鏁般傞偅杩欎釜鍥炲ご璋冪敤鍙堟槸浠涔堥锛

鍏堟潵鐪嬬湅鏉ヨ嚜缁村熀鐧剧鐨勫鍥炶皟锛圕allback锛夌殑瑙f瀽锛欼n computer programming, a callback is any executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at a given time. This execution may be immediate as in a synchronous callback, or it might happen at a later time as in an asynchronous callback. 涔熷氨鏄锛屾妸涓娈靛彲鎵ц鐨勪唬鐮佸儚鍙傛暟浼犻掗偅鏍蜂紶缁欏叾浠栦唬鐮侊紝鑰岃繖娈典唬鐮佷細鍦ㄦ煇涓椂鍒昏璋冪敤鎵ц锛岃繖灏卞彨鍋氬洖璋冦快三投注网平台—官方网址22270.COM濡傛灉浠g爜绔嬪嵆琚墽琛屽氨绉颁负鍚屾鍥炶皟锛屽鏋滃湪涔嬪悗鏅氱偣鐨勬煇涓椂闂村啀鎵ц锛屽垯绉颁箣涓哄紓姝ュ洖璋冦快三投注网平台—官方网址22270.COM鍏充簬鍚屾鍜屽紓姝ワ紝杩欓噷涓嶄綔璁ㄨ锛岃鏌ラ槄鐩稿叧璧勬枡銆

鍐嶆潵鐪嬬湅鏉ヨ嚜Stack Overflow鏌愪綅澶х绠娲佹槑浜嗙殑琛ㄨ堪锛A "callback" is any function that is called by another function which takes the first function as a parameter銆 涔熷氨鏄锛屽嚱鏁 F1 璋冪敤鍑芥暟 F2 鐨勬椂鍊欙紝鍑芥暟 F1 閫氳繃鍙傛暟缁 鍑芥暟 F2 浼犻掍簡鍙﹀涓涓嚱鏁 F3 鐨勬寚閽堬紝鍦ㄥ嚱鏁 F2 鎵ц鐨勮繃绋嬩腑锛屽嚱鏁癋2 璋冪敤浜嗗嚱鏁 F3锛岃繖涓姩浣滃氨鍙仛鍥炶皟锛圕allback锛夛紝鑰屽厛琚綋鍋氭寚閽堜紶鍏ャ佸悗闈㈠張琚洖璋冪殑鍑芥暟 F3 灏辨槸鍥炶皟鍑芥暟銆傚埌姝ゅ簲璇ユ槑鐧藉洖璋冨嚱鏁扮殑瀹氫箟浜嗗惂锛

2. 涓轰粈涔堣浣跨敤鍥炶皟鍑芥暟锛

寰堝鏈嬪弸鍙兘浼氭兂锛屼负浠涔堜笉鍍忔櫘閫氬嚱鏁拌皟鐢ㄩ偅鏍凤紝鍦ㄥ洖璋冪殑鍦版柟鐩存帴鍐欏嚱鏁扮殑鍚嶅瓧鍛紵杩欐牱涓嶄篃鍙互鍚楋紵涓轰粈涔堥潪寰楃敤鍥炶皟鍑芥暟鍛紵鏈夎繖涓兂娉曞緢濂斤紝鍥犱负鍦ㄧ綉涓婄湅鍒拌В鏋愬洖璋冨嚱鏁扮殑寰堝渚嬪瓙锛屽叾瀹炲畬鍏ㄥ彲浠ョ敤鏅氬嚱鏁拌皟鐢ㄦ潵瀹炵幇鐨勩傝鍥炵瓟杩欎釜闂锛屾垜浠厛鏉ヤ簡瑙d竴涓嬪洖鍒板嚱鏁扮殑濂藉鍜屼綔鐢紝閭e氨鏄В鑰︼紝瀵癸紝灏辨槸杩欎箞绠鍗曠殑绛旀锛屽氨鏄洜涓鸿繖涓壒鐐癸紝鏅氬嚱鏁颁唬鏇夸笉浜嗗洖璋冨嚱鏁般快三投注网平台—官方网址22270.COM鎵浠ワ紝鍦ㄦ垜鐪奸噷锛岃繖鎵嶆槸鍥炶皟鍑芥暟鏈澶х殑鐗圭偣銆傛潵鐪嬬湅缁村熀鐧剧涓婇潰鎴戣寰楃敾寰楀緢濂界殑涓寮犲浘鐗囥

涓嬮潰浠ヤ竴娈典笉瀹屾暣鐨 C 璇█浠g爜鏉ュ憟鐜颁笂鍥剧殑鎰忔濓細

瀹炰緥

#include<stdio.h>
#include<softwareLib.h> // 鍖呭惈Library Function鎵鍦ㄨ寰桽oftware library搴撶殑澶存枃浠

int Callback() // Callback Function
{
    // TODO
    return 0;
}
int main() // Main program
{
    // TODO
    Library(Callback);
    // TODO
    return 0;
}

涔嶄竴鐪嬶紝鍥炶皟浼间箮鍙槸鍑芥暟闂寸殑璋冪敤锛屽拰鏅氬嚱鏁拌皟鐢ㄦ病鍟ュ尯鍒紝浣嗕粩缁嗕竴鐪嬶紝鍙互鍙戠幇涓よ呬箣闂寸殑涓涓叧閿殑涓嶅悓锛氬湪鍥炶皟涓紝涓荤▼搴忔妸鍥炶皟鍑芥暟鍍忓弬鏁颁竴鏍蜂紶鍏ュ簱鍑芥暟銆傝繖鏍蜂竴鏉ワ紝鍙鎴戜滑鏀瑰彉浼犺繘搴撳嚱鏁扮殑鍙傛暟锛屽氨鍙互瀹炵幇涓嶅悓鐨勫姛鑳斤紝杩欐牱鏈夋病鏈夎寰楀緢鐏垫椿锛熷苟涓斾笣姣笉闇瑕佷慨鏀瑰簱鍑芥暟鐨勫疄鐜帮紝杩欏氨鏄В鑰︺傚啀浠旂粏鐪嬬湅锛屼富鍑芥暟鍜屽洖璋冨嚱鏁版槸鍦ㄥ悓涓灞傜殑锛岃屽簱鍑芥暟鍦ㄥ彟澶栦竴灞傦紝鎯充竴鎯筹紝濡傛灉搴撳嚱鏁板鎴戜滑涓嶅彲瑙侊紝鎴戜滑淇敼涓嶄簡搴撳嚱鏁扮殑瀹炵幇锛屼篃灏辨槸璇翠笉鑳介氳繃淇敼搴撳嚱鏁拌搴撳嚱鏁拌皟鐢ㄦ櫘閫氬嚱鏁伴偅鏍峰疄鐜帮紝閭f垜浠氨鍙兘閫氳繃浼犲叆涓嶅悓鐨勫洖璋冨嚱鏁颁簡锛岃繖涔熷氨鏄湪鏃ュ父宸ヤ綔涓父瑙佺殑鎯呭喌銆傜幇鍦ㄥ啀鎶妋ain()銆丩ibrary()鍜孋allback()鍑芥暟濂楀洖鍓嶉潰 F1銆丗2鍜孎3鍑芥暟閲岄潰锛屾槸涓嶆槸灏辨洿鏄庣櫧浜嗭紵

鏄庣櫧浜嗗洖璋冨嚱鏁扮殑鐗圭偣锛屾槸涓嶆槸涔熷彲浠ュぇ姒傜煡閬撳畠搴旇鍦ㄤ粈涔堟儏鍐典笅浣跨敤浜嗭紵娌¢敊锛屼綘鍙互鍦ㄥ緢澶氬湴鏂逛娇鐢ㄥ洖璋冨嚱鏁版潵浠f浛鏅氱殑鍑芥暟璋冪敤锛屼絾鏄湪鎴戠湅鏉ワ紝濡傛灉闇瑕侀檷浣庤﹀悎搴︾殑鏃跺欙紝鏇村簲璇ヤ娇鐢ㄥ洖璋冨嚱鏁般

3. 鎬庝箞浣跨敤鍥炶皟鍑芥暟锛

快三投注网平台—官方网址22270.COM鐭ラ亾浜嗕粈涔堟槸鍥炶皟鍑芥暟锛屼簡瑙d簡鍥炶皟鍑芥暟鐨勭壒鐐癸紝閭d箞搴旇鎬庝箞浣跨敤鍥炶皟鍑芥暟锛熶笅闈㈡潵鐪嬩竴娈电畝鍗曠殑鍙互鎵ц鐨勫悓姝ュ洖璋冨嚱鏁颁唬鐮併

瀹炰緥

#include<stdio.h>

int Callback_1() // Callback Function 1
{
    printf("Hello, this is Callback_1 ");
    return 0;
}

int Callback_2() // Callback Function 2
{
    printf("Hello, this is Callback_2 ");
    return 0;
}

int Callback_3() // Callback Function 3
{
    printf("Hello, this is Callback_3 ");
    return 0;
}

int Handle(int (*Callback)())
{
    printf("Entering Handle Function. ");
    Callback();
    printf("Leaving Handle Function. ");
}

int main()
{
    printf("Entering Main Function. ");
    Handle(Callback_1);
    Handle(Callback_2);
    Handle(Callback_3);
    printf("Leaving Main Function. ");
    return 0;
}

杩愯缁撴灉锛

Entering Main Function.
Entering Handle Function.
Hello, this is Callback_1
Leaving Handle Function.
Entering Handle Function.
Hello, this is Callback_2
Leaving Handle Function.
Entering Handle Function.
Hello, this is Callback_3
Leaving Handle Function.
Leaving Main Function.

鍙互鐪嬪埌锛孒andle()鍑芥暟閲岄潰鐨勫弬鏁版槸涓涓寚閽堬紝鍦╩ain()鍑芥暟閲岃皟鐢℉andle()鍑芥暟鐨勬椂鍊欙紝缁欏畠浼犲叆浜嗗嚱鏁癈allback_1()/Callback_2()/Callback_3()鐨勫嚱鏁板悕锛岃繖鏃跺欑殑鍑芥暟鍚嶅氨鏄搴斿嚱鏁扮殑鎸囬拡锛屼篃灏辨槸璇达紝鍥炶皟鍑芥暟鍏跺疄灏辨槸鍑芥暟鎸囬拡鐨勪竴绉嶇敤娉曘傜幇鍦ㄥ啀璇讳竴閬嶈繖鍙ヨ瘽锛欰 "callback" is any function that is called by another function which takes the first function as a parameter锛屾槸涓嶆槸灏辨洿鏄庣櫧浜嗗憿锛

4. 鎬庝箞浣跨敤甯﹀弬鏁扮殑鍥炶皟鍑芥暟锛

快三投注网平台—官方网址22270.COM鐪煎皷鐨勬湅鍙嬪彲鑳藉彂鐜颁簡锛屽墠闈㈢殑渚嬪瓙閲岄潰鍥炶皟鍑芥暟鏄病鏈夊弬鏁扮殑锛岄偅涔堟垜浠兘涓嶈兘鍥炶皟閭d簺甯﹀弬鏁扮殑鍑芥暟鍛紵绛旀鏄偗瀹氱殑銆傞偅涔堟庝箞璋冪敤鍛紵鎴戜滑绋嶅井淇敼涓涓嬩笂闈㈢殑渚嬪瓙灏卞彲浠ヤ簡锛

瀹炰緥

#include<stdio.h>

int Callback_1(int x) // Callback Function 1
{
    printf("Hello, this is Callback_1: x = %d ", x);
    return 0;
}

int Callback_2(int x) // Callback Function 2
{
    printf("Hello, this is Callback_2: x = %d ", x);
    return 0;
}

int Callback_3(int x) // Callback Function 3
{
    printf("Hello, this is Callback_3: x = %d ", x);
    return 0;
}

int Handle(int y, int (*Callback)(int))
{
    printf("Entering Handle Function. ");
    Callback(y);
    printf("Leaving Handle Function. ");
}

int main()
{
    int a = 2;
    int b = 4;
    int c = 6;
    printf("Entering Main Function. ");
    Handle(a, Callback_1);
    Handle(b, Callback_2);
    Handle(c, Callback_3);
    printf("Leaving Main Function. ");
    return 0;
}

杩愯缁撴灉锛

Entering Main Function.
Entering Handle Function.
Hello, this is Callback_1: x = 2
Leaving Handle Function.
Entering Handle Function.
Hello, this is Callback_2: x = 4
Leaving Handle Function.
Entering Handle Function.
Hello, this is Callback_3: x = 6
Leaving Handle Function.
Leaving Main Function.

鍙互鐪嬪埌锛屽苟涓嶆槸鐩存帴鎶奿nt Handle(int (*Callback)()) 鏀规垚 int Handle(int (*Callback)(int)) 灏卞彲浠ョ殑锛岃屾槸閫氳繃鍙﹀澧炲姞涓涓弬鏁版潵淇濆瓨鍥炶皟鍑芥暟鐨勫弬鏁板硷紝鍍忚繖閲 int Handle(int y, int (*Callback)(int)) 鐨勫弬鏁 y銆傚悓鐞嗭紝鍙互浣跨敤澶氫釜鍙傛暟鐨勫洖璋冨嚱鏁般

鍘熸枃鍦板潃锛歨ttp://www.cnblogs.com/jiangzhaowei/p/9129105.html