有人可以用真正简单的语言向我解释什么是闭包吗?

发布于 2024-08-10 18:23:15 字数 222 浏览 5 评论 0原文

可能的重复:
.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

此岸叶落 2024-08-17 18:23:15

我想说这是重复的: 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."

饭团 2024-08-17 18:23:15

你的鞋子在大厅里;你的
夹克在厨房里。把它们
戴上,还有你的手套(它们在
抽屉),外出时。

现在你可以去玩你的车了。
十一点你必须去买点东西
街角商店里的面包。

孩子玩。忘记了世界的一切。

闹钟响了;孩子看到:十一点了!哦 - 使用“外出”封闭装置去外面买面包。

Your shoes are in the hall; your
jacket is in the kitchen. Put them
on, and your gloves (they're in the
drawer), when going outside.

Now you can go playing with your cars.
At eleven o'clock you must go buy some
bread in the corner store.

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.

初懵 2024-08-17 18:23:15

我喜欢 Javascript 的 Google 示例(您可以对其进行变形)对于 C# 来说很容易)。这不是 5 岁孩子能理解的东西,但我怀疑平均 5 岁孩子是否会理解函数是什么。

/*
* When a function is defined in another function and it
*    has access to the outer function's context even after
*    the outer function returns
* An important concept to learn in Javascript
*/

function outerFunction(someNum) {
  var someString = 'Hai!';
  var content = document.getElementById('content');
  function innerFunction() {
    content.innerHTML = someNum + ': ' + someString;
    content = null; // IE memory leak for DOM reference
  }
  innerFunction();
}

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.

/*
* When a function is defined in another function and it
*    has access to the outer function's context even after
*    the outer function returns
* An important concept to learn in Javascript
*/

function outerFunction(someNum) {
  var someString = 'Hai!';
  var content = document.getElementById('content');
  function innerFunction() {
    content.innerHTML = someNum + ': ' + someString;
    content = null; // IE memory leak for DOM reference
  }
  innerFunction();
}
无风消散 2024-08-17 18:23:15

下面的答案是对原始措辞的回应,类似于“如何向 5 岁的孩子解释闭包”。

拿走这盒乐高积木;建立你自己
一个漂亮的小太空飞船。当你走的时候
到比利家并带上你的空间
那里有工艺;通过闭包你可以
仍然可以使用你的所有部分
一盒乐高积木,即使盒子被留下了
在你的卧室里。

The below answer was to the original wording which was akin to "How to explain closures to a 5-year old."

Take this box of legos; build yourself
a nice little space craft. When you go
to billy's house and bring your space
craft there; with closures you can
still can use all the pieces in your
box of legos, even though the box was left
in your bedroom.

秋千易 2024-08-17 18:23:15

如果您确实需要保持简单,那么闭包就是一个具有上下文的函数。闭包中的函数仍然可以访问与定义时相同的变量,无论您从哪里调用它。 (在 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.

焚却相思 2024-08-17 18:23:15

如果 5 岁的孩子会 C#,我会用这个代码示例来解释:

int i = 0;
string result = null;
Action iExaminer = () =>
{
  result = i % 2 == 1 ? "Odd" : "Even";
};
i = 1;
iExaminer();
Console.WriteLine(result);

如果 5 岁的孩子正在学习 linq,我会用这个代码示例来解释:

string name = null;
IEnumerable<Customer> query = Customers.Where(c => c.Name == name);
name = "Bob";
 // query is resolved when enumerated (which is now)
 // Where will now call our anonymous method.
foreach(var customer in query)
{
  Console.WriteLine(customer.Name);
}

If the 5 year old knew C#, I would explain with this code sample:

int i = 0;
string result = null;
Action iExaminer = () =>
{
  result = i % 2 == 1 ? "Odd" : "Even";
};
i = 1;
iExaminer();
Console.WriteLine(result);

If the 5 year old was learning linq, I would explain with this code sample:

string name = null;
IEnumerable<Customer> query = Customers.Where(c => c.Name == name);
name = "Bob";
 // query is resolved when enumerated (which is now)
 // Where will now call our anonymous method.
foreach(var customer in query)
{
  Console.WriteLine(customer.Name);
}

闭包(计算机科学) 说:

在计算机科学中,闭包是第一个 -具有绑定在词法环境中的自由变量的类函数。

翻译:
闭包关闭/附加函数周围的变量,以便该函数可以传送到其他地方并仍然使用这些变量
例如,假设您被传送到一个远程位置,但仍然可以访问放在桌子上的咖啡杯

示例:

function makefunc(x)
{
    return function(){return x}
}

现在使用 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:

function makefunc(x)
{
    return function(){return x}
}

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

撑一把青伞 2024-08-17 18:23:15

当您大致知道如何做某事时,您可以指定一些(或全部)细节并获得一个闭包

例如,您知道如何购买冰淇淋。如果您将在任何商店前,您知道该怎么做。但如果您想去一家特定商店(例如,由于周日折扣),您就会搬出家,目的是在那里购买冰淇淋。 “在街角的商店买一些冰淇淋”是“买一些冰淇淋”的结束语。其实,这一切都是“去哪儿买点冰淇淋”的闭包:

  • 在街角买点冰淇淋
  • 买两个冰淇淋
  • 在街角买两个冰淇淋

现在去和你的朋友们一起玩吧,儿子! (我记住不要在孩子们面前说类似的话)

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":

  • Buy some ice-cream at the corner
  • Buy two ice-creams
  • Buy two ice-creams at the corner

Now go play with your friends, son! (and I bear in mind not say anything like that in front of the children)

审判长 2024-08-17 18:23:15

这是 C# 中实现该想法的简单方法: Closure

This is a simple approach to the idea in C#: Closure

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文