c#学习笔记之九 事件(event)的使用,一个简单的例子揭示其设计理念

什么是事件?事件就是一系列的动作。比如,柚子表白事件,就有好几个动作:1.摆柚子2.暖场舞蹈3.拿话筒表白。

理解事件是一系列动作,对理解事件的使用很关键。好了,事件是由一系列动作组成。那么动作是什么呢?动作就是函数,因为函数就是做什么,一个函数就是一个动作,反之亦然。因此,事件是由一系列函数组成的。那么,函数有大有小,把一系列的函数实体加入到事件中,是不明智的。我们就将函数的代表加入到事件中。什么是函数的代表呢?在C#中,有一个delegate 关键词,它就是标记一个函数代表类的。

delegate void FunctionName(string s);

函数代表,可以看做是函数名,函数名就唯一代表了一个函数,这是讲得通的。

既然要使用事件,就要先定义一个事件类。因为在C#中,将事件也是看作对象的,既然有对象,那就有类。

定义一个事件类的时候:

当然要包含一个事件,是个什么事件,具有事件——动作列表属性。另外,还要有一个方法,来运行这个事件。

在使用一个事件的时候:

首先新建一个事件对象。在事件中加入动作代表。这样,一个事件就构造完毕了。后面的,就是运行这个事件了。

下面的代码很好地阐述了我对C#中event的理解。

 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2016/11/9
 * Time: 19:22
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;

namespace EventDemo
{

delegate void FunctionName();<strong>//declare a funtion delegate class</strong>
class EventClass      {
	public event FunctionName someevent;<strong>//declare an event;the event based on function delegate;</strong>
	public void Run()<strong>//the method that makes the event run;</strong>
		{
		someevent();<strong>//because someevent is function name,so,this fashion makes function run;</strong>
		 }
	                   }  
<strong>//following is some functions that should be used directly in future;
//hence,i wrap them in a class;</strong>
class F		  {
	public static void Arrange()
		{
		Console.WriteLine("the boy arranges the grapefuits");
		 }
	public static void Dance()
		{
		Console.WriteLine("friends of the boy dance");
		 }
	public static void Express()
		{
		Console.WriteLine("the boy sings \"if you\" " );
		 }
			   }

class Program    {
	public static void Main(string[] args)
		{  
		EventClass myEvent = new EventClass();
		//add function name,namely,the function delegates, to the event;
       //someevent is the list of functions;and functions are represented by function delegates;</strong>
        myEvent.someevent += F.Dance;
		myEvent.someevent += F.Arrange;
		myEvent.someevent += F.Express;
		myEvent.Run(); //event occurs;
		Console.ReadKey(true);
	     } 
                  }
}


其实,如果有C编程的基础,知道C中指针数组的用法;在加上懂一点函数指针的概念。理解事件(event)和函数代表(delegate)是 一点都不难的。

对event的理解,可以持更为包容和开放的态度。event既然是一系列函数的列表,也可以看作是方法。既然是方法,那么它就属于某个对象的。那么,它就应该定义在类里面。作为类的一个方法使用。函数列表的函数可以来自很多方面,只要是函数就可以了。

using System;

namespace EventDemo
{
public delegate void Handler();
class Fighter{               //可以在一个一般定义类中定义一个事件,即函数列表,作为类的对象的方法使用
	public string name;
	public event Handler attackevent;
	public void Attack()
		{
		attackevent();
		}
             }
class F{
	public static void React()
		 {
		Console.WriteLine("sense the atmosphere and make judgement instantly");
		  }
        }
class Program  {
	static void Attack()
		{
		Console.WriteLine("twist waist and launch the strength");
		 }
	public static void Main(string[] args)
		{
		Fighter f = new Fighter();
		f.name = "cgguang";//对对象的成员变量赋值,为了显示事件所在的类是一个一般类,没有特别
		f.attackevent += F.React;//事件,即函数列表中的函数来源多样。这个是来自类F;
		f.attackevent += Program.Attack;//事件,即函数列表中的函数来源多样。这个是来自类Program;
		f.Attack();//一系列函数执行
		Console.Write("Press any key to continue . . . ");
		Console.ReadKey(true);
		 }
                }
}


  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值