代表是什么意思?为什么我们需要它?

发布于 2024-10-01 08:13:42 字数 316 浏览 1 评论 0原文

可能的重复:
代表们,我无法理解他们

嗨朋友们,

什么目标C中的委托是什么意思?为什么我们需要它?我们什么时候应该使用它?里面有什么类型吗?如何使用?

请朋友们用简单的文字和例子来解释一下。我看了很多文章,论坛..但我仍然听不懂它的确切解释..

谢谢

Possible Duplicate:
Delegates, can't get my head around them

Hi friends,

What is mean by delegate in objective C? Why we need it? When should we use it? Is there any types in it? How to use it?

Please friends, use simple words and examples to explain. I saw so many articles, forums.. But still i can not catch the exact explanation of it..

Thank you

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

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

发布评论

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

评论(1

初熏 2024-10-08 08:13:43

委托是一种修改类行为而不需要对类进行子类化的方法。通常,您不想显着改变行为,而是稍微调整一下;子类化太过分了,所以这就是代表发挥作用的地方。

这样看:一个青少年代表一个阶级,她的父母是代表。少女的朋友打电话让她去商场逛逛,但少女必须先询问她的父母是否可以。家长——代表——可以说“是”或“否”。这就是 Cocoa 中代表的工作方式。

里面有什么类型吗?

代表通常可以是任何类型。在 10.6 中,许多委托通过可选方法实现协议,因此您会看到类似 id 的类型,但这在 10.6 之前并不常见。

A delegate is a way to modify the behavior of a class without requiring the class to be subclassed. Often you don't want to dramatically change behavior, but tweak it a bit; subclassing would be overkill, so that's where delegates come in to play.

Look at it this way: a teenager represents a class, and her parent the delegate. The teenager's friend calls her to come hang out at the mall, but the teenager has to ask her parents if it's okay first. The parent -- the delegate -- can say yes or no. That's how delegates work in Cocoa.

Is there any types in it?

Delegates can generally be of any type. In 10.6, many delegates implement protocols with optional methods, so you'll see types like id <BlahClassDelegate>, but that wasn't common before 10.6.

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