Delegate&Event

Delegate

1.基本类:

 1     public class Student
 2     {
 3         public int Id { get; set; }
 4         public string Name { get; set; }
 5
 6         public void Study()
 7         {
 8             Console.WriteLine("学习.net高级班公开课");
 9         }
10
11         public void StudyAdvanced(string name)
12         {
13             Console.WriteLine("学习.net高级班公开课");
14         }
15
16         public static void Show()
17         {
18             Console.WriteLine("123");
19         }
20     }

Student

2. 声明委托

1         public delegate void NoReturnNoPara();
2         public delegate void NoReturnWithPara(int x, int y);
3         public delegate int WithReturnNoPara();
4         public delegate string WithReturnWithPara(int x, int y);    

3. =+ 的方法

 1         private void DoNothing()
 2         {
 3             Console.WriteLine("这里是DoNothing");
 4         }
 5
 6         private static void DoNothingStatic()
 7         {
 8             Console.WriteLine("这里是DoNothing");
 9         }
10
11         private void ShowPlus(int x, int y)
12         {
13             Console.WriteLine("ShowPlus x={0} y={1} x+y={2}", x, y, x + y);
14         }
15
16         private static void ShowPlusStatic(int x, int y)
17         {
18             Console.WriteLine("ShowPlusStatic x={0} y={1} x+y={2}", x, y, x + y);
19         }    

4.委托三部曲

声明(在上面)+实例化+调用

1            {
2                 NoReturnNoPara method = new NoReturnNoPara(this.DoNothing);//2 委托的实例化
3                 method.Invoke();//3 委托调用
4                 method();//另一种调用方式
5                 //method.BeginInvoke(null, null);//异步调用
6                 this.DoNothing();//直接调用对应的方法,效果一样
7             }    

5.多播委托

 1          {
 2                 NoReturnNoPara method = new NoReturnNoPara(MyDelegate.DoNothingStatic);
 3                 method += student.Study;
 4                 method -= student2.Study;
 5                 method += this.DoNothing;
 6                 method += student.Study;
 7                 method += Student.Show;
 8                 method += () => Console.WriteLine("这里lambda");
 9                 method.Invoke();
10                 //method.BeginInvoke(null,null);//多播委托不能异步
11                 method -= MyDelegate.DoNothingStatic;
12                 method -= MyDelegate.DoNothingStatic;
13                 method -= this.DoNothing;
14                 method -= student.Study;
15                 method -= Student.Show;
16                 method -= () => Console.WriteLine("这里lambda");
17
18                 //委托里面lambda表达式之后加入的方法都无效了?
19                 method.Invoke();
20             }
21             {
22                 NoReturnNoPara method = new NoReturnNoPara(student.Study);
23                 method.Invoke();
24             }
25             {
26                 NoReturnNoPara method = new NoReturnNoPara(Student.Show);
27                 method.Invoke();
28             }
29             {
30                 NoReturnWithPara method = new NoReturnWithPara(ShowPlus);
31                 method += ShowPlus;
32                 method += ShowPlus;
33                 method += ShowPlus;
34                 method -= ShowPlus;
35                 method -= ShowPlus;
36                 method(1, 2);
37             }    

Event

1    public class Baby
2     {
3         public void Cry()
4         {
5             Console.WriteLine("{0} Cry", this.GetType().Name);
6         }
7     }

Baby

1      public class Brother
2     {
3         public void Turn()
4         {
5             Console.WriteLine("{0} Turn", this.GetType().Name);
6         }
7     }

Brother

1     public class Dog
2     {
3         public void Wang()
4         {
5             Console.WriteLine("{0} Wang", this.GetType().Name);
6         }
7     }

Dog

1     public class Father
2     {
3         public void Shout()
4         {
5             Console.WriteLine("{0} Shout", this.GetType().Name);
6         }
7     }

Father

1     public class Mother
2     {
3         public void Wispher()
4         {
5             Console.WriteLine("{0} Wispher", this.GetType().Name);
6         }
7     }

Mother

1     public class Mouse
2     {
3         public void Run()
4         {
5             Console.WriteLine("{0} Run", this.GetType().Name);
6         }
7     }

Mouse

1     public class Neighbor
2     {
3         public void Awake()
4         {
5             Console.WriteLine("{0} Awake", this.GetType().Name);
6         }
7     }

Neighbor

1     public class Stealer
2     {
3         public void Hide()
4         {
5             Console.WriteLine("{0} Hide", this.GetType().Name);
6         }
7     }

Stealer

                              发布

 1     /// <summary>
 2     /// 发布者
 3     /// 一只猫,miao一声
 4     /// 导致一系列的触发动作
 5     /// </summary>
 6     public class Cat
 7     {
 8         public void Miao()
 9         {
10             Console.WriteLine("{0} Miao", this.GetType().Name);
11             new Neighbor().Awake();
12             new Mouse().Run();
13             new Dog().Wang();
14             new Baby().Cry();
15             new Mother().Wispher();
16             new Brother().Turn();
17             new Father().Shout();
18         }
19
20         public Action MiaoAction;//委托的实例
21         public event Action MiaoActionEvent;//事件是 委托的实例,加了event关键字-----发布者
22         //event做了权限控制,保证外部不能赋值和调用
23         public void MiaoDelegate()
24         {
25             Console.WriteLine("{0} MiaoDelegate", this.GetType().Name);
26
27             MiaoAction.Invoke();
28         }
29
30         public void MiaoEvent()
31         {
32             Console.WriteLine("{0} MiaoEvent", this.GetType().Name);
33             if (MiaoActionEvent != null)
34             {
35                 //MiaoActionEvent.Invoke();
36                 //MiaoActionEvent();
37                 foreach (Action item in MiaoActionEvent.GetInvocationList())//触发
38                 {
39                     Console.WriteLine("*********");
40                     item.Invoke();
41                     //item.BeginInvoke(null,null);
42                 }
43             }
44         }
45     }

订阅:

 1           {
 2                     Console.WriteLine("*******Miao******");
 3                     Cat cat = new Cat();
 4                     cat.Miao();
 5
 6                     Console.WriteLine("*******MiaoDelegate******");
 7                     //cat.MiaoAction += new Neighbor().Awake;
 8
 9                     cat.MiaoAction += new Mouse().Run;
10                     cat.MiaoAction -= new Mouse().Run;
11                     cat.MiaoAction += new Baby().Cry;
12                     cat.MiaoAction += new Mother().Wispher;
13
14                     cat.MiaoAction.Invoke();
15                     cat.MiaoAction = null;
16
17                     cat.MiaoAction += new Brother().Turn;
18                     cat.MiaoAction += new Father().Shout;
19                     cat.MiaoAction += new Dog().Wang;
20
21                     cat.MiaoDelegate();
22
23                     Console.WriteLine("*******MiaoEvent******");
24                     cat.MiaoActionEvent += new Neighbor().Awake;
25                     cat.MiaoActionEvent += new Mouse().Run;
26                     cat.MiaoActionEvent += new Baby().Cry;
27
28                     //cat.MiaoActionEvent.Invoke();
29                     //cat.MiaoActionEvent = null;
30
31                     cat.MiaoActionEvent += new Mother().Wispher;
32                     cat.MiaoActionEvent += new Brother().Turn;
33                     cat.MiaoActionEvent += new Father().Shout;
34                     cat.MiaoActionEvent += new Dog().Wang;
35                     cat.MiaoEvent();
36                 }
时间: 2024-08-11 09:56:28

Delegate&Event的相关文章

delegate, event - 里面涉及的参数类型必须完全一致,子类是不行的

public void TestF() { Test += Fun; } public void Fun(Person p) { }  // 如 Person变成 SubPerson,则报错..public void Fun(SubPerson p) { } public event Action<Person> Test; delegate, event - 里面涉及的参数类型必须完全一致,子类是不行的

c# delegate ,event

首先说明,event其实也是一种delegate,为了区分,我们称一般的delegate为"plain delegate". 写代码的过程中,经常会在delegate和event之间进行选择,以前也没仔细思考选择的原因,今天终于忍不住花了半天时间仔细琢磨了一下--好了,直接拿代码说话吧: using System;namespace EventAndDelegate{    public delegate void TestDelegate(string s);    public i

delegate&amp;event学习理解

一. //声明的委托 public delegate void BoilerLogHandler(string status); // 基于上面的委托定义事件 public event BoilerLogHandler BoilerEventLog; //main方法 static void Main(string[] args) { BoilerInfoLogger filelog = new BoilerInfoLogger(@"E:\\boiler.txt"); Delegate

c#中的delegate(委托)和event(事件)

委托: 托付其他人做这件事   ,包括 托付自己  ,即  一个方法 可以  调用 没有关系的其他方法 , 也可以 将委托传递过去 ,回调自己的方法 ,且 可以自定义参数 ,非常方便 互相传值, 适合解耦 关系. 示例: public delegate void ChangeMoney(object s, int n);   // 用 delegate  声明委托 1. 调用 其他方法 售卖 页面添加商品,添加 的 商品 在另一个页面也能看见 . 售卖页面 类里面 定义委托: //定义一个委托 

C#学习日记24----事件(event)

事件为类和类的实例提供了向外界发送通知的能力,实现了对象与对象之间的通信,如果定义了一个事件成员,表示该类型具有 1.能够在事件中注册方法 (+=操作符实现). 2.能够在事件中注销方法(-=操作符实现). 3.当事件被触发时注册的方法会被通知(事件内部维护了一个注册方法列表).委托(Delegate)是事件(event)的载体,要定义事件就的要有委托.  有关委托的内容请点击 委托(De... www.mafengwo.cn/event/event.php?iid=4971258www.maf

Liferay7 BPM门户开发之4: Activiti事件处理和监听Event handlers

事件机制从Activiti 5.15开始引入,这非常棒,他可以让你实现委托. 可以通过配置添加事件监听器,也可以通过Runtime API加入注册事件. 所有的事件参数子类型都来自org.activiti.engine.delegate.event.ActivitiEvent 包含的信息: type executionId processInstanceId processDefinitionId 事件监听 其中,JOB_EXECUTION_SUCCESS 和JOB_EXECUTION_FAIL

C# 部分语法总结(入门经典)

class Program { static void Main(string[] args) { init(); System.Console.ReadKey(); } #region 接口 /// <summary> /// 接口成员是公共的,不能加其他修饰符,如static,public,virtual等 /// </summary> public interface IA { string IStr { get; set; } void IMethod(); } publi

target/action 设计模式

Target-Action模式是ObjC里非常常见的对象之间方法调用的方式,不过ObjC把方法调用叫做Send Message. 一帮情况在和UI打交道时候处理各种GUI上的事件会利用到这种模式.相对应的.NET上的处理模式就是delegate/event了. 不过,Target-Action拜C语言所赐,更是灵活很多,编译期没有任何检查,都是运行时的绑定. 看代码 首先我们创建一个继承UIView的类 1 #import <UIKit/UIKit.h> 2 3 @interface Touc

DOM事件简介

Click.touch.load.drag.change.input.error.risize — 这些都是冗长的DOM(文档对象模型)事件列表的一部分.事件可以在文档(Document)结构的任何部分被触发,触发者可以是用户操作,也可以是浏览器本身.事件并不是只是在一处被触发和终止:他们在整个document中流动,拥有它们自己的生命周期.而这个生命周期让DOM事件有更多的用途和可扩展性. 作为一个开发人员,我们必须要理解DOM事件是如何工作的,然后才能更好的驾驭它,利用它们潜在的优势,开发出