如何用简单的英语解释回调?它们与从一个函数调用另一个函数从调用函数获取上下文有什么不同?如何向新手程序员解释它们的强大功能?
当前回答
用电话系统来描述回调是最容易的。函数调用类似于打电话给某人,问她一个问题,得到答案,然后挂断电话;添加回调改变了这个类比,这样在问她一个问题之后,你也可以告诉她你的名字和电话号码,这样她就可以给你回电话,告诉你答案。——保罗·雅库比 、《c++中的回调实现》
其他回答
您有一些想要运行的代码。通常,当你调用它时,你会等待它在你继续之前完成(这可能会导致你的应用程序变灰/产生游标的旋转时间)。
另一种方法是并行运行此代码并继续您自己的工作。但是,如果原始代码需要根据它所调用的代码的响应做不同的事情,该怎么办?在这种情况下,您可以传递您希望它在完成时调用的代码的名称/位置。这是一个“回电”。
正常代码:询问信息->处理信息->处理结果->继续做其他事情。
使用回调:Ask for Information->Process Information->继续执行其他操作。在稍后的某个点->处理处理的结果。
在PHP中,它是这样的:
<?php
function string($string, $callback) {
$results = array(
'upper' => strtoupper($string),
'lower' => strtolower($string),
);
if(is_callable($callback)) {
call_user_func($callback, $results);
}
}
string('Alex', function($name) {
echo $name['lower'];
});
当我们有两个函数,函数a和函数b,如果函数a依赖于函数b。
然后我们调用函数b作为回调函数。这在Spring框架中被广泛使用。
简单明了:回调是你给另一个函数的函数,这样它就可以调用它。
通常在某个操作完成时调用它。由于在将回调函数交给其他函数之前创建了回调,因此可以使用调用站点的上下文信息初始化它。这就是为什么它被命名为call*back* -第一个函数从它被调用的地方回调到上下文。
为了教授回调,你必须先教授指针。一旦学生理解了指向变量的指针的概念,回调的概念就会变得更容易。假设您使用的是C/ c++,可以遵循这些步骤。
First show your students how to use and manipulate variables using pointers alongside using the normal variable identifiers. Then teach them there are things that can be done only with pointers(like passing a variable by reference). Then tell them how executable code or functions are just like some other data(or variables) in the memory. So, functions also have addresses or pointers. Then show them how functions can be called with function pointers and tell these are called callbacks. Now, the question is, why all these hassle for calling some functions? What is the benefit? Like data pointers, function pointer aka callbacks has some advantages over using normal identifiers. The first one is, function identifiers or function names cannot be used as normal data. I mean, you cannot make a data structure with functions(like an array or a linked list of functions). But with callbacks, you can make an array, a linked list or use them with other data like in dictionary of key-value pairs or trees, or any other things. This is a powerful benefit. And other benefits are actually child of this one. The most common use of callbacks is seen in event driver programming. Where one or more functions are executed based on some incoming signal. With callbacks, a dictionary can be maintained to map signals with callbacks. Then the input signal resolution and execution of corresponding code become much easier. The second use of callbacks coming in my mind is higher order functions. The functions which takes other functions as input arguments. And to send functions as arguments, we need callbacks. An example can be a function which take an array and a callback. Then it performs the callback on each of the item of the array and return the results in another array. If we pass the function a doubling callback, we get a doubled valued array. If we pass a squaring callback, we get squares. For square roots, just send appropriate callback. This cannot be done with normal functions.
可能还有更多的事情。让学生参与进来,他们就会发现。希望这能有所帮助。