可能有人给我解释一下到底什么是一个事件,是与代表不同? [英] could someone explain to me what exactly is an event and what is the difference with delegate?

查看:228
本文介绍了可能有人给我解释一下到底什么是一个事件,是与代表不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
什么是委托和事件之间的区别?

我不明白委托和事件

推荐答案

之间的差异假设您的企业有一个客户,矮牵牛德思礼。矮牵牛是女性,住在4号女贞路。

Suppose your business has a customer, Petunia Dursley. Petunia is female and lives at #4 Privet Drive.

您可能想的代表的客户用的对象的在您的计算机系统,让你做一个Customer类拥有的属性的如姓名和地址和性。也许,名称及地址都是字符串,也许是性是一个枚举。

You might want to represent your customers with an object in your computer system, so you make a Customer class that has properties like "Name" and "Address" and "Sex". Perhaps Name and Address are both strings, perhaps "Sex" is an enum.

现在,是矮牵牛地址的字符串?当然不是。这是一个地址。它的代表的在你的模型String类型的的对象的。由于矮牵牛人有类似的名称,地址和性别属性,请在系统中相应的对象也有属性名称,地址和性别。

Now, is Petunia's address a string? Of course not. It's an address. It is represented by an object of type String in your model. Because Petunia the person has properties like name, address and sex, the corresponding object in your system also has properties Name, Address and Sex.

在类属性是在商业领域,一个东西的属性模型,如客户的属性的。 的实现该属性的机制的选择是基于什么是最便民的建设和使用软件的模式。在这种情况下,可能的机制是一堆的各种类型的字段,以使得属性包含访问字段代码。这些都是的机制的实现的属性。

A property in the class is a model of a property of a thing in the business domain, such as a property of customer. The choice of the mechanisms that implement that property is based on what is most convenient for the people building and using the software model. In this case, probably the mechanism is a bunch of fields of various types such that the properties contain code that accesses the field. Those are mechanisms that implement the properties.

活动和代表有同样的关系。 事件是发生的事情,你想了解被告知软件表示。委托是对的机制的实现的事件的。

Events and delegates have the same relationship. An "event" is a software representation of "something that happens that you'd like to be informed about". A delegate is the mechanism that implements the event.

一个按钮可以点击;这是一个关于按钮的事实。这一事实是在软件中通过一个Click事件为蓝本。当点击该按钮,的机制的,告知已发生的单击事件感兴趣的各方是的代表的。

A button can be clicked; that's a fact about buttons. That fact is modeled in software by a "Click" event. When the button is clicked, the mechanism that informs interested parties that the click event has occurred is a delegate.

总结:事件和属性用来表示在类型的业务领域的的概念。字段(通常)的特性用做他们的工作,代表们(总是)机制的机制的该事件使用做好自己的工作。

Summing up: Events and properties are used to represent concepts in the business domain of the type. Fields are (usually) the mechanism that properties use to do their work, and delegates are (always) the mechanism that events use to do their work.

清楚了吗?

这篇关于可能有人给我解释一下到底什么是一个事件,是与代表不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆