需要“委托”的简洁定义

发布于 2024-09-10 12:48:50 字数 49 浏览 1 评论 0原文

我需要一个关于“委托”在编程中如何工作的简短描述,最好用一句话来描述。更好地使用类比

I need a short description of how "delegation" works in programming, preferably in one sentence. Even better to use analogies

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

滥情哥ㄟ 2024-09-17 12:48:50

类型安全的函数指针

A type safe function pointer.

吹梦到西洲 2024-09-17 12:48:50

最好使用类比

代表就像暴力。如果他们还没有解决你的问题,那么你还没有使用足够的东西。

好吧,这个笑话不是原创,所以起诉我吧。

现在说真的。代表就像……代表。因此得名。现实生活中的代表是什么? 代表是代表他人行事的人。您委派工作给他们,然后他们执行该工作。您想要交易 XYZ 公司的 50 股,您不必亲自去交易大厅进行交易,而是将这项工作委托给为您做的经纪人;他们如何做到这一点,只要它完成了,你就不在乎。经纪人是您的代表;他们代表您完成工作。

委托是一个代表按需工作能力的对象。您创建一个执行特定工作的委托人,当您希望完成该工作时,您可以要求您的委托人为您做这件事。

Even better to use analogies

Delegates are like violence. If they haven't solved your problem yet then you haven't used enough.

OK, this joke is not original, so sue me.

Seriously now. Delegates are like... delegates. Hence the name. What is a delegate in real life? A delegate is someone who acts on behalf of another. You delegate work to them and they perform that work. You want to trade fifty shares of XYZ corp, you don't go down to the trading floor and do it yourself, you delegate that work to your broker who does it for you; how they do it, you don't care so long as it gets done. The broker is your delegate; they do the work on your behalf.

A delegate is an object that represents the ability to do work on demand. You create a delegate that does a particular job, and when you want the job done, you ask your delegate to go do it for you.

如何视而不见 2024-09-17 12:48:50

委托将方法包装到类型安全的对象引用中,就像啤酒开瓶器包装方法一样 (开啤酒)在一个方便的工具(啤酒开瓶器),可反复用于不同类型的啤酒瓶。

为啤酒开瓶器定义了输入,

注意:这不是 99 瓶啤酒;)

注意 (2):您可以 不用啤酒开瓶器打开啤酒,就像您可以在没有委托的情况下使用方法(甚至其引用)一样。它只是更加混乱和不太清晰,但它可以更有趣。

A delegate wraps a method into a type safe object reference the way a beer opener wraps a method (opening beer) in a handy tool (the beer opener) that can be used again and again for different kind of beer bottles.

There's input defined for the beer opener, a certain type of bottle and a force (your hands). It holds the key to an action of which the form is always the same. Likewise, there's input defined for the delegate, which holds the key, a contract, to an action (showing a form) with certain input (the form).

Note: this is not about 99 bottles of beer ;)

Note (2): you can open a beer without a beer opener, the same way you can use a method (even its reference) without a delegate. It's just messier and less clear, but it can be a lot more fun though.

我早已燃尽 2024-09-17 12:48:50

我能想到的最好的类比来自 C 术语,即“函数指针”。

这个想法是,您需要能够调用一段代码,但是您要调用的那段代码直到运行时才知道。因此,您可以使用“委托”来实现此目的。

The best analogy I can think of comes from the C terminology, which is "pointer to function".

The idea is that you need to be able to invoke a piece of code, but that piece of code you're going to invoke isn't known until runtime. So you use a "delegate" for that purpose.

§普罗旺斯的薰衣草 2024-09-17 12:48:50

委托是引用方法的类型。

A delegate is a type that references a method.

梦纸 2024-09-17 12:48:50

代表就像实习生,就像你说“嘿,我需要一名实习生给我送杯咖啡。”

幸运的话,你会遇到一个能够执行“给我一杯咖啡”的人。不同的实习生可能会以不同的方式执行该任务,例如,实习生可能会跑去星巴克。另一个人可能会到处跑,直到她在大楼的某个地方找到一个非空的咖啡壶,然后给你倒一杯。

A delegate is like an intern, as in when you say "Hey, I need an intern to bring me a cup of coffee."

With luck you will get someone who is capable of executing "bring me a cup of coffee". Different interns might execute that task differently, one may run out to Starbucks, for example. Another might run around until she finds a non-empty coffee pot somewhere in the building and pour you a cup.

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