国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

回調(diào)函數(shù)

2019-11-11 02:02:20
字體:
供稿:網(wǎng)友

翻編程書籍的時候,往往都會提到回調(diào)函數(shù),到底回調(diào)函數(shù)和普通的函數(shù)調(diào)用有什么區(qū)別,google和baidu了一大堆之后寫一寫自己的理解,中英文同時舉例相信應(yīng)該能夠說明的更加清楚的

what is callback function?

quote from wikipedia page:

In computer PRogramming, a callback is areference to executable code, or a piece of executable code, that is passed asan argument to other code. This allows a lower-level software layer to call asubroutine (or function) defined in a higher-level layer.

這是維基百科上的解釋,主要說明了callback是作為參數(shù)傳入進去的,能夠讓一個高層的函數(shù)調(diào)用底層的函數(shù)

但是,沒有把callback的實際應(yīng)用場景說明白

舉個簡單的例子:

我現(xiàn)在很想買書,跑到網(wǎng)上各種找,都沒有這本書,于是我在網(wǎng)上留下了我的聯(lián)系方式,等到網(wǎng)上的書到貨之后(用專業(yè)術(shù)語說是某個特定的事件發(fā)生之后,英文用event),然后工作人員給我打電話通知書到了(事件發(fā)生之后調(diào)用callback函數(shù))你還買不買?

英文舉例(要習(xí)慣于去看英文的解釋,中文的很多翻譯都不靠譜,包括我自己的)

I am going to try to keep this deadsimple. A "callback" is any function that is called by anotherfunction which takes the first function as a parameter. A lot of the time, a"callback" is a function that is called when something happens. That something can be called an "event" inprogrammer-speak.

Imagine this scenario: You are expecting a package in acouple of days. The package is a gift for your neighbor. Therefore, once youget the package, you want it brought over to the neighbors. You are out oftown, and so you leave instructions for your spouse.

You could tell them to get the package and bring it tothe neighbors. If your spouse was as stupid as a computer, they would sit atthe door and wait for the package until it came (NOT DOING ANYTHING ELSE) andthen once it came they would bring it over to the neighbors. But there's abetter way. Tell your spouse that ONCE they receive the package, they shouldbring it over the neighbors. Then, they can go about life normally UNTIL shereceives the package.

In our example, the receiving of thepackage is the "event" and the bringing it to the neighbors is the"callback". Your spouse "runs" your instructions to bringthe package over only when thepackage arrives. Much better!

那么,我們使用callback函數(shù)的目的是什么呢?個人認為是,因為我當(dāng)前不知道我需要的那個事件什么時候發(fā)生(書什么時候到),而且我需要去做別的事情(不能一直傻傻的看著書店的貨源看看我的書到?jīng)]到),而是我自己正常做自己的事情,而等到書到了之后(事件已經(jīng)發(fā)生了),有人自然會跟我打電話的(調(diào)用了回調(diào)函數(shù))

Why would you want to do this? Let's say thereis a service you need to invoke. If the service returns immediately, you just:

Call it

Wait for the result

Continue once the result comes in

Just understand that it is just a name todescribe a method that's supplied as an argument to another method, such thatwhen the parent method is called (whatever condition, such as a button click, atimer tick etc) and its method body completes, the callback method is theninvoked, or in other Words "called at the back" of the other function.

個人理解:callback和calling的區(qū)別是:

A:calling我知道什么時候調(diào)用的,而callback不知道(不知道什么時候函數(shù)執(zhí)行,因為事件不知道什么時候發(fā)生,事件發(fā)生之后,函數(shù)執(zhí)行,所以也可能函數(shù)不執(zhí)行)

B:callback是被動調(diào)用的(事件發(fā)生之后才會執(zhí)行),而calling是主動的

C:calling我知道調(diào)用的是哪個方法,而callback是不知道的(是用戶傳入的),在我買書的例子中,我想留下自己的聯(lián)系方法,如果我留下的是QQ,工作人員需要調(diào)用我的QQ聊天的方法,如果我留下的是郵箱,工作人員需要調(diào)用發(fā)郵件的方法;如果我留下的是電話,需要調(diào)用打電話的方法。而這些方法,都應(yīng)該是我(買書的用戶)已經(jīng)實現(xiàn)好的,而工作人員(調(diào)用者)只需要調(diào)用我的方法即可。

How are they different from calling onefunction from another function taking some context from the calling function?

It is true that you are calling a functionfrom another function, but the key is that the callback is treated like anObject, so you can change which Function to call based on the state of thesystem (like the Strategy Design Pattern).


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 鄂伦春自治旗| 尉氏县| 泾川县| 扶风县| 横山县| 紫阳县| 云浮市| 托克逊县| 夏津县| 运城市| 新龙县| 正镶白旗| 龙山县| 防城港市| 深水埗区| 安陆市| 武冈市| 南丰县| 慈溪市| 奉贤区| 延川县| 宾阳县| 南投县| 梧州市| 毕节市| 武清区| 桂东县| 来凤县| 贵州省| 孟村| 安康市| 若羌县| 山东| 米泉市| 宁武县| 民乐县| 凉城县| 栾川县| 义马市| 西藏| 桂林市|