委托,一些代码示例?对象如何委托给其他对象

发布于 2024-08-04 22:44:20 字数 48 浏览 3 评论 0原文

我想对代表团有更好的了解。有人可以粘贴一个好的委托代码示例并解释它是如何工作的吗?

I would like to gain a better understanding about the delegation. Can somebody please paste a good code sample of delegation and explain how it works?

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

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

发布评论

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

评论(5

謌踐踏愛綪 2024-08-11 22:44:20

有一个很好的例子: http://en.wikipedia.org/wiki/ delegate_pattern#Objective-C_example

在此示例中,MyCoolAppController 创建 TCScrollView 类型的对象,并设置 TCScrollView 的“delegate”属性代码> 到<代码>自我。这意味着当 TCScrollView 调用时,

[delegate scrollView:self shouldScrollToPoint:to]

它会要求 MyCoolAppController(TCScrollView 的委托)执行一些计算并查看是否可以滚动。你可以说“MyCoolAppController is the delegate of TCScrollView”来描述这一点; TCScrollView 要求 MyCoolAppController 代表它做一些工作。

There is a pretty good example at: http://en.wikipedia.org/wiki/Delegation_pattern#Objective-C_example

In this example, MyCoolAppController creates and object of type TCScrollView, and sets the "delegate" property of the TCScrollView to self. This means that when the TCScrollView calls

[delegate scrollView:self shouldScrollToPoint:to]

it is asking the MyCoolAppController (the delegate of the TCScrollView) to perform some calculations and see if it is ok to scroll. You can say "MyCoolAppController is the delegate of TCScrollView" to describe this; TCScrollView asks MyCoolAppController to do some work on its behalf.

还给你自由 2024-08-11 22:44:20

您是指 .NET、Java 还是其他语言委托?

Do you mean .NET or Java or some other language delegate?

极度宠爱 2024-08-11 22:44:20

.NET 术语中的委托只不过是一个函数指针,或者换句话说,是一个指向可执行代码块的变量。它们可以以多种方式使用。一种方法是在事件上下文中使用它们。假设您有一个 ASP.NET 页面,并且正在使用 MVP(该页面上的模型视图演示者模式)。您希望演示者收到视图上保存按钮的单击事件的通知。您可以在视图接口上定义一个事件,但为了订阅该事件并对其采取操作,您需要注册一个在引发事件时触发的方法。例如:

public class ClassThatRegistersForEvent
{
   public void InitializeView(IView view)
   {
      view.SaveButtonClickedEvent += delegate{
                                        // do stuff in here when the event is raised
                                     }
   }
}
public interface IView
{
  event System.EventHandler SaveButtonClickedEvent;
}

A delegate in .NET parlance is nothing more than a function pointer, or in other words a variable that points to a block of executable code. They can be used in may ways. One way is to use them in the context of events. Lets say you have an ASP.NET page and you are using the MVP (Model View Presenter pattern on that page). You want your presenter to be notified of the click event of the save button on the view. You can define an event on the views interface, but in order to subscribe to that event and to take action on it you need to register a method that gets fired when the event is raised. For example:

public class ClassThatRegistersForEvent
{
   public void InitializeView(IView view)
   {
      view.SaveButtonClickedEvent += delegate{
                                        // do stuff in here when the event is raised
                                     }
   }
}
public interface IView
{
  event System.EventHandler SaveButtonClickedEvent;
}
紫竹語嫣☆ 2024-08-11 22:44:20

这是我写的解释委托的答案:https://stackoverflow.com/questions/1089737#1090170

Here's an answer I wrote explaining delegation: https://stackoverflow.com/questions/1089737#1090170

九厘米的零° 2024-08-11 22:44:20

委托是响应事件的一种方式。在其他语言中,您可能会通过子类化来做到这一点。例如,假设您有一个表视图。您可以子类化 tableview 并覆盖 tableView:didSelectRowAtIndexPath: 方法,但这会变得混乱并创建不必要的子类(以及它不可重用的事实)相反,您创建一个 TableViewDelegate 类并告诉您的表视图(tableView 。代表)。这样,当有事情发生时,该方法就会自动被调用。这是一个非常干净的事件处理解决方案。

当你编写了一些涉及委托的应用程序(表视图是最重要的)后,你就会掌握它的窍门。

A delegate is a way to respond to events. In other languages you would probably do this by subclassing. For example, say you have a table view. You could subclass the tableview and override the tableView:didSelectRowAtIndexPath: method, but that would get messy and create an unnecessary subclass (along with the fact that its not reusable) Instead, you create a TableViewDelegate class and tell your table view about it (tableView.delegate). This way, the method will automatically get called when something happens. This is a really clean solution to event-handling.

After you write a few apps that involve delegates (table views are the big ones), you'll get the hang of it.

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