什么是回调方法?
我是一个编程菜鸟,不太理解回调方法背后的概念。尝试在维基百科上阅读有关它的内容,但它超出了我的想象。有人可以用简单的术语解释一下吗?
I'm a programming noob and didn't quite understand the concept behind callback methods. Tried reading about it in wiki and it went over my head. Can somebody please explain this in simple terms?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
回调是传递给函数的东西,它告诉函数在操作的某个时刻应该调用什么。函数中的代码决定何时调用该函数(以及传递哪些参数)。通常,在函数为对象的语言中,执行此操作的方法是将函数本身作为“回调”传递。在其他语言中,您可能必须传递某种称为“函数指针”(或类似)的特殊事物;或者您可能必须传递函数的名称(然后在运行时查找该名称)。
Python 中的一个简单示例:
这个示例让我们将重复函数调用的任务与实际调用函数的任务分开。这不是很有用,但它演示了这个概念。
在现实世界中,回调经常用于诸如线程库之类的事情,您可以在其中使用描述线程将执行的工作的回调来调用一些线程创建函数。线程创建函数执行设置线程所需的工作,然后安排新线程调用回调函数。
The callback is something that you pass to a function, which tells it what it should call at some point in its operation. The code in the function decides when to call the function (and what arguments to pass). Typically, the way you do this is to pass the function itself as the 'callback', in languages where functions are objects. In other languages, you might have to pass some kind of special thing called a "function pointer" (or similar); or you might have to pass the name of the function (which then gets looked up at runtime).
A trivial example, in Python:
This example let us separate the task of repeating a function call, from the task of actually calling the function. That's not very useful, but it demonstrates the concept.
In the real world, callbacks are used a lot for things like threading libraries, where you call some thread-creation function with a callback that describes the work that the thread will do. The thread-creation function does the necessary work to set up a thread, and then arranges for the callback function to be called by the new thread.
维基百科说:
通俗地说,它是一种在需要时通知一段代码(即一个方法)要执行哪一段代码(即另一个方法)的机制。
回调与以下事实有关:调用函数的客户端指定一个属于客户端代码负责的函数来执行调用函数,并将其作为参数传递。
GUI 就是一个例子。您将在事件发生(例如按下按钮)后调用的函数作为参数传递,并且一旦事件发生
发生此函数被调用。
该功能通常由最初注册该事件的对象来实现
Wiki says:
In common terms it is the mechanism to notify a piece of code i.e. a method, which piece of code to execute, i.e. another method, when it is needed.
Callback is related to the fact that the client of the calling function specifies a function that belongs to the client code's responsibility to the calling function to execute and this is passed as an argument.
An example is in GUIs. You pass as argument the function to be called once an event occurs (e.g. button pressed) and once the event
occurs this function is called.
This function is usually implemented by the object that originally registered for the event
回调函数是通过函数指针调用的函数。如果将一个函数的指针(地址)作为参数传递给另一个函数,那么当该指针用于调用它所指向的函数时,就称为进行了回调。
为什么应该使用回调函数?
回调可用于通知。例如,您需要在应用程序中设置一个计时器。每次计时器到期时,都必须通知您的应用程序。但是,time'rs 机制的实现者对您的应用程序一无所知。它只需要一个指向具有给定原型的函数的指针,并在使用该指针时进行回调,通知您的应用程序已发生的事件。事实上,SetTimer() WinAPI 使用回调函数来通知计时器已过期(并且,如果没有提供回调函数,它会将消息发布到应用程序的队列)。
Callback function is a function that is called through a function pointer. If you pass the pointer (address) of a function as an argument to another, when that pointer is used to call the function it points to it is said that a call back is made.
Why Should You Use Callback Functions?
A callback can be used for notifications. For instance, you need to set a timer in your application. Each time the timer expires, your application must be notified. But, the implementer of the time'rs mechanism doesn't know anything about your application. It only wants a pointer to a function with a given prototype, and in using that pointer it makes a callback, notifying your application about the event that has occurred. Indeed, the SetTimer() WinAPI uses a callback function to notify that the timer has expired (and, in case there is no callback function provided, it posts a message to the application's queue).
一般来说,您提供一个函数作为参数,当发生某些事情时该函数会被调用。
在 C 代码中,您将传递如下所示的内容:
表示一个带有两个参数(void * 和 long)并返回 int 的函数。
对于面向对象语言,语法有时更简单。例如,您也许能够构建一个回调机制,允许用户传入一个看起来像函数或具有抽象方法(从而包装函数)的对象以及上下文数据。
现代语言使用术语“委托”来指代函数“模式”。这些可以用作回调。有些语言还使用术语 lambda ,它本质上是一个没有名称的函数,通常在代码块中“动态”创建并作为回调传递。
C++11
已将这些引入其标准中。使用回调的优点是您可以分离出来,即减少/解耦 API 与调用它的对象,并且在某种程度上反之亦然,即尽管在某个地方您知道您正在调用 API,但在这一点上“处理程序”不需要知道它是从哪里调用的。
例如,您可以使用一个 API 来生成对象,然后在生成对象时进行“回调”。
In general you supply a function as a parameter which gets called when something occurs.
In C code you will pass something that looks like this:
meaning a function that takes two parameters, void * and long and returns an int.
With object-orientated languages the syntax is sometimes simpler. For example you might be able to construct a callback mechanism that allows the user to pass in an object that looks like a function or has an abstract method (thus wrapping a function) and context data too.
Modern languages use the term "delegate" to refer to a function "pattern". These can be used as callbacks. Some languages also use the term
lambda
which is essentially a function with no name, often created "on the fly" in a block of code and passed as a callback.C++11
has introduced these into its standard.The advantage of using a callback is that you can separate out, i.e. reduce / decouple an API from what is calling it, and to some extent vice versa, i.e. although in one place you know you are calling into the API, at the point of the "handler" it does not need to know from where it was called.
For example, you can have an API that generates objects and then "calls-back" as they get generated.
回调意味着您将代码作为参数传递。例如,想象一个按钮,按下时会显示一个对话框:
这里我们告诉按钮要执行什么,何时单击。因此,当框架检测到点击时,它将执行传递的代码。框架内部有一些代码,例如:
因此,一些基本代码会在适当的事件发生时回调侦听器。
PS:这里是伪代码,只是描述一下想法。
Call back means that you pass the code as a parameter. For example, imagine a button, that much show a dialog when pressed:
Here we tell the button what to execute, when it will be click. So, the framework will execute the passed code, when it detecs the click. Inside the framework there are some code like:
So, some basic code calls back the listener when the appropriate event happens.
PS: Pseudocode here, just do describe the idea.
回调方法是当事件发生时被调用的方法
A callback method is a method that gets called when an event occurs
简而言之,实际上,回调是对函数或方法的引用,您可以将其作为参数传递给另一个函数以便稍后回调它。
从上图中,
B_reference()
就是回调方法。源代码示例:
如果您还是不明白它是什么,可以查看此视频:
In simple word, actually, a Callback is a reference to a function or method, which you can pass as a parameter to another function for calling it back later.
From the above figure,
B_reference()
is the callback method.Source code sample:
If you still don't understand what it is, you can check this video: