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

首頁 > 學院 > 開發設計 > 正文

回調函數

2019-11-11 02:03:19
字體:
來源:轉載
供稿:網友

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

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是作為參數傳入進去的,能夠讓一個高層的函數調用底層的函數

但是,沒有把callback的實際應用場景說明白

舉個簡單的例子:

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

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

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函數的目的是什么呢?個人認為是,因為我當前不知道我需要的那個事件什么時候發生(書什么時候到),而且我需要去做別的事情(不能一直傻傻的看著書店的貨源看看我的書到沒到),而是我自己正常做自己的事情,而等到書到了之后(事件已經發生了),有人自然會跟我打電話的(調用了回調函數)

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的區別是:

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

B:callback是被動調用的(事件發生之后才會執行),而calling是主動的

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

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).


上一篇:MySQL主從設置

下一篇:面向過程(2)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 辽阳县| 麻栗坡县| 白山市| 瑞昌市| 濉溪县| 黑龙江省| 西峡县| 台湾省| 天津市| 富锦市| 津市市| 渭南市| 石阡县| 乳山市| 浪卡子县| 阿拉善盟| 象山县| 台北县| 民县| 大庆市| 色达县| 锡林郭勒盟| 卓尼县| 辽阳县| 湘潭县| 屏南县| 水城县| 搜索| 额尔古纳市| 翁牛特旗| 金乡县| 金昌市| 盈江县| 扬中市| 平定县| 广州市| 玉树县| 永年县| 莆田市| 新疆| 长顺县|