理解委托与事件的好文章
2005-09-26 15:57
381 查看
1.委派的实现过程。
首先来看一下委派,委派其实就是方法的传递,并不定义方法的实现。事件其实就是标准化了的委派,为了事件处理过程特制的、稍微专业化一点的组播委派(多点委派)。下面举一个例子,我觉得把委派的例子和事件的例子比较,会比较容易理解。
using System;[/b]
[/b]
[/b]class Class1[/b]
[/b]{[/b]
[/b] delegate int MathOp(int i1,int i2);[/b]
[/b] static void [/b]Main[/b](string[] args)[/b]
[/b] {[/b]
MathOp op1=new MathOp(Add);[/b]
MathOp op2=new MathOp(Multiply);[/b]
Console.WriteLine(op1(100,200));[/b]
[/b] Console.WriteLine(op2(100,200));[/b]
Console.ReadLine();[/b]
[/b] }[/b]
[/b] public static int Add(int i1,int i2)[/b]
[/b] {[/b]
return i1+i2;[/b]
[/b] }[/b]
[/b] public static int Multiply(int i1,int i2)[/b]
[/b] {[/b]
[/b] return i1*i2;[/b]
[/b] }[/b]
}[/b]
首先代码定义了一个委托MathOp,其签名匹配与两个函数Add()和Multiply()的签名(也就是其带的参数类型数量相同):
delegate int MathOp(int i1,int i2);
Main()中代码首先使用新的委托类型声明一个变量,并且初始化委托变量.注意,声明时的参数只要使用委托传递的函数的函数名,而不加括号:
MathOp op1=new MathOp(Add);
(或为MathOp op1=new MathOp(Multiply);)
委托传递的函数的函数体:
public static int Add(int i1,int i2)
{
return i1+i2;
}
public static int Multiply(int i1,int i2)
{
return i1*i2;
}
然后把委托变量看作是一个函数名,将参数传递给函数。 Console.WriteLine(op1(100,200));
Console.WriteLine(op2(100,200));
2.事件的实现过程
using System;
class Class1
{
static void Main(string[] args)
{
Student s1=new Student();
Student s2=new Student();
s1.RegisterOK +=new Student.DelegateRegisterOkEvent(Student_RegisterOK);
s2.RegisterOK +=new Student.DelegateRegisterOkEvent(Student_RegisterOK);
s1.Register();
s2.Register();
Console.ReadLine();
}
static void Student_RegisterOK()
{
Console.WriteLine("Hello");
}
}
class Student
{
public delegate void DelegateRegisterOkEvent();
public event DelegateRegisterOkEvent RegisterOK;
public string Name;
public void Register()
{
Console.WriteLine("Register Method");
RegisterOK();
}
}
在Student类中,先声明了委托DelegateRegisterOkEvent(),然后使用event和要使用的委托类型(前面定义的DelegateRegisterOkEvent委托类型)声明事件RegisterOK(可以看作是委托的一个实例。):
public delegate void DelegateRegisterOkEvent();[/b]
public event DelegateRegisterOkEvent RegisterOK;[/b]
然后在Main()函数中,实例化Student类,然后s1.RegisterOK事件委托给了Student_RegisterOK 方法。通过“+=”(加等于)操作符非常容易地为.Net对象中的一个事件添加一个甚至多个响应方法;还可以通过非常简单的“-=”(减等于)操作符取消这些响应方法。
然后,当调用s1.Register()时,事件s1.RegisterOK发生。
首先来看一下委派,委派其实就是方法的传递,并不定义方法的实现。事件其实就是标准化了的委派,为了事件处理过程特制的、稍微专业化一点的组播委派(多点委派)。下面举一个例子,我觉得把委派的例子和事件的例子比较,会比较容易理解。
using System;[/b]
[/b]
[/b]class Class1[/b]
[/b]{[/b]
[/b] delegate int MathOp(int i1,int i2);[/b]
[/b] static void [/b]Main[/b](string[] args)[/b]
[/b] {[/b]
MathOp op1=new MathOp(Add);[/b]
MathOp op2=new MathOp(Multiply);[/b]
Console.WriteLine(op1(100,200));[/b]
[/b] Console.WriteLine(op2(100,200));[/b]
Console.ReadLine();[/b]
[/b] }[/b]
[/b] public static int Add(int i1,int i2)[/b]
[/b] {[/b]
return i1+i2;[/b]
[/b] }[/b]
[/b] public static int Multiply(int i1,int i2)[/b]
[/b] {[/b]
[/b] return i1*i2;[/b]
[/b] }[/b]
}[/b]
首先代码定义了一个委托MathOp,其签名匹配与两个函数Add()和Multiply()的签名(也就是其带的参数类型数量相同):
delegate int MathOp(int i1,int i2);
Main()中代码首先使用新的委托类型声明一个变量,并且初始化委托变量.注意,声明时的参数只要使用委托传递的函数的函数名,而不加括号:
MathOp op1=new MathOp(Add);
(或为MathOp op1=new MathOp(Multiply);)
委托传递的函数的函数体:
public static int Add(int i1,int i2)
{
return i1+i2;
}
public static int Multiply(int i1,int i2)
{
return i1*i2;
}
然后把委托变量看作是一个函数名,将参数传递给函数。 Console.WriteLine(op1(100,200));
Console.WriteLine(op2(100,200));
2.事件的实现过程
using System;
class Class1
{
static void Main(string[] args)
{
Student s1=new Student();
Student s2=new Student();
s1.RegisterOK +=new Student.DelegateRegisterOkEvent(Student_RegisterOK);
s2.RegisterOK +=new Student.DelegateRegisterOkEvent(Student_RegisterOK);
s1.Register();
s2.Register();
Console.ReadLine();
}
static void Student_RegisterOK()
{
Console.WriteLine("Hello");
}
}
class Student
{
public delegate void DelegateRegisterOkEvent();
public event DelegateRegisterOkEvent RegisterOK;
public string Name;
public void Register()
{
Console.WriteLine("Register Method");
RegisterOK();
}
}
在Student类中,先声明了委托DelegateRegisterOkEvent(),然后使用event和要使用的委托类型(前面定义的DelegateRegisterOkEvent委托类型)声明事件RegisterOK(可以看作是委托的一个实例。):
public delegate void DelegateRegisterOkEvent();[/b]
public event DelegateRegisterOkEvent RegisterOK;[/b]
然后在Main()函数中,实例化Student类,然后s1.RegisterOK事件委托给了Student_RegisterOK 方法。通过“+=”(加等于)操作符非常容易地为.Net对象中的一个事件添加一个甚至多个响应方法;还可以通过非常简单的“-=”(减等于)操作符取消这些响应方法。
然后,当调用s1.Register()时,事件s1.RegisterOK发生。
相关文章推荐
- 【转】理解委托与事件的好文章
- 理解委托和事件的好文章http://www.cnblogs.com/JimmyZhang/archive/2007/09/23/903360.html
- C#委托及事件委托的初步理解(经典文章)
- 对ASP.NET中关于事件委托的理解(转)
- 深入理解.net的事件与委托机制
- 【MS】关于委托、事件、泛型文章整理
- 理解JavaScript中的事件路由冒泡过程及委托代理机制
- 委托、事件的个人理解
- C#委托很经典的例子,叫你理解委托与事件
- 理解C#中的委托和事件
- C#中委托、事件和回调函数的理解
- asp.net事件委托易理解实例
- C#中,对委托和事件的理解
- C#事件-换一个角度理解委托和事件
- C# 如何理解委托事件(二)
- 对事件与委托的理解(2) 引发事件
- 从callback的角度来理解.NET/C# 中的 委托 (delegate)与 事件 (event)
- 对C#委托和事件委托的理解
- 具体代码,理解委托与事件
- 俗人理解的事件和委托