有人可以用真正简单的语言向我解释什么是闭包吗?
可能的重复:
.NET 中的“闭包”是什么?
我目前正在研究 lambda 表达式和结束语不断出现。有人可以用真正简单的语言向我解释一下吗?
Possible Duplicate:
What are ‘closures’ in .NET?
I am currently looking at lambda expression and the word closure keeps coming. Can someone explain it to me in real simple language.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我想说这是重复的: What are 'closures' in .NET?
“本质上,闭包是一个可以稍后执行的代码块,但它维护了最初创建它的环境 - 即它仍然可以使用创建它的方法的局部变量等,甚至该方法执行完毕后。”
I'd say this is a duplicate of: What are ‘closures’ in .NET?
"In essence, a closure is a block of code which can be executed at a later time, but which maintains the environment in which it was first created - i.e. it can still use the local variables etc of the method which created it, even after that method has finished executing."
孩子玩。忘记了世界的一切。
闹钟响了;孩子看到:十一点了!哦 - 使用“外出”封闭装置去外面买面包。
Kid plays. Forgets all the world.
Alarm clock goes off; kid sees: eleven o'clock! Oh - go outside to buy bread using the "going outside" closure.
我喜欢 Javascript 的 Google 示例(您可以对其进行变形)对于 C# 来说很容易)。这不是 5 岁孩子能理解的东西,但我怀疑平均 5 岁孩子是否会理解函数是什么。
I like the Google example for Javascript (you can morph it for C# easily). It's not something a 5 year old would understand but then I doubt an average 5 year old would understand what a function was.
下面的答案是对原始措辞的回应,类似于“如何向 5 岁的孩子解释闭包”。
The below answer was to the original wording which was akin to "How to explain closures to a 5-year old."
如果您确实需要保持简单,那么闭包就是一个具有上下文的函数。闭包中的函数仍然可以访问与定义时相同的变量,无论您从哪里调用它。 (在 Lua 中,这些被称为 upvalues,我认为这是一个非常具有描述性的术语。)
我首先在 Lua 中遇到了这个概念,这个定义帮助我理解了这个概念。也许看看 Lua:它的简单和强大令人着迷,并且肯定有助于形成对其他语言的某种看法。它的闭包概念就是一个很好的例子。
If you really need to keep it simple, then a closure is a function with its context. The function in the closure can still access the same variables it could when it was defined, no matter where you call it from. (In Lua, these are called upvalues, which I think is a very descriptive term.)
I met the concept first in Lua, and this definition helped me understand the concept. Maybe have a look at Lua: its simpleness and power is fascinating, and certainly helps to develop a certain view at other languages. Its concept of closures would be a good example to that.
如果 5 岁的孩子会 C#,我会用这个代码示例来解释:
如果 5 岁的孩子正在学习 linq,我会用这个代码示例来解释:
If the 5 year old knew C#, I would explain with this code sample:
If the 5 year old was learning linq, I would explain with this code sample:
闭包(计算机科学) 说:
在计算机科学中,闭包是第一个 -具有绑定在词法环境中的自由变量的类函数。
翻译:
闭包关闭/附加函数周围的变量,以便该函数可以传送到其他地方并仍然使用这些变量
例如,假设您被传送到一个远程位置,但仍然可以访问放在桌子上的咖啡杯
示例:
现在使用 makefunc,您可以创建许多匿名函数,这些函数将返回您传递给 makefunc 的内容
因此,如果如果你想要一个返回 10 的函数,请使用 makefunc(10)(),尽管返回 10 的方法非常无用:)
Closure (computer science) says:
In computer science, a closure is a first-class function with free variables that are bound in the lexical environment.
Translation:
Closures close/attach the variables around the function, so that that function can be teleported to somewhere else and still use those variables
e.g. suppose you are teleported to a remote location but have still access to your coffed mug lying on your table
Example:
Now using makefunc, you can make many anonymous functions which will return what you pass to makefunc
So if you want a function which returns 10, use makefunc(10)(), though pretty useless way toget back 10 :)
当您大致知道如何做某事时,您可以指定一些(或全部)细节并获得一个闭包。
例如,您知道如何购买冰淇淋。如果您将在任何商店前,您知道该怎么做。但如果您想去一家特定商店(例如,由于周日折扣),您就会搬出家,目的是在那里购买冰淇淋。 “在街角的商店买一些冰淇淋”是“买一些冰淇淋”的结束语。其实,这一切都是“去哪儿买点冰淇淋”的闭包:
现在去和你的朋友们一起玩吧,儿子! (我记住不要在孩子们面前说类似的话)
When you know how to do something in general, you can specify some (or all) details and get a closure.
For example, you know how to buy ice-cream. Yyou know what to do if you will be in front of any shop. But if you want to go to a particular shop (for example, due to a Sunday discount), you move out of house with the aim of buying ice-cream there. "Buy some ice-cream at a store on the corner" is a closure of "buy some ice-cream". In fact, all these are closures of "buy some ice-cream somewhere":
Now go play with your friends, son! (and I bear in mind not say anything like that in front of the children)
这是 C# 中实现该想法的简单方法: Closure
This is a simple approach to the idea in C#: Closure