国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

一個簡單的小例子讓你明白c#中的委托-終于懂了!

2019-11-14 16:29:03
字體:
來源:轉載
供稿:網友

模擬主持人發布一個問題,由多個嘉賓來回答這個問題。

 

分析:從需求中抽出Host (主持人) 類和Guests (嘉賓) 類。

作為問題的發布者,Host不知道問題如何解答。因此它只能發布這個事件,將事件委托給多個嘉賓去處理。因此在Host 類定義事件,在Guests類中定義事件的響應方法。通過多番委托的"+="將響應方法添加到事件列表中,最終 Host 類將觸發這個事件。實現過程如下:

 

代碼其實很少下面貼出來所有代碼:

 

QuestionArgs.cs

 

view plaincopy to clipboardPRint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     public class QuestionArgs:EventArgs  
  9.     {  
  10.         public string Message { get; set; }  
  11.     }  
  12. }  
[csharp] view plaincopyprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     public class QuestionArgs:EventArgs  
  9.     {  
  10.         public string Message { get; set; }  
  11.     }  
  12. }  

 

 

 

 

 

Program.cs

 

view plaincopy to clipboardprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             Host host = new Host();  
  13.             host.Name = "主持人";  
  14.             host.args.Message = "C#的事件如何實現的?";  
  15.             Guests[] gArray = new Guests[3]  
  16.             {  
  17.                 new GuestA(){Name = "張小三"},  
  18.                 new GuestB(){Name = "李小四"},  
  19.                 new GuestC(){Name = "王老五"}  
  20.             };  
  21.             //用+=號,將嘉賓的答題方法加入到委托鏈  
  22.             host.QuestionEvent += new QuestionHandler(gArray[0].answer);  
  23.             host.QuestionEvent += new QuestionHandler(gArray[1].answer);  
  24.             host.QuestionEvent += new QuestionHandler(gArray[2].answer);  
  25.   
  26.             //觸發事件   
  27.             host.StartAnswer();  
  28.             Console.ReadLine();  
  29.         }  
  30.     }  
  31. }<span style="color:#ff0000;">  
  32. </span>  
[csharp] view plaincopyprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             Host host = new Host();  
  13.             host.Name = "主持人";  
  14.             host.args.Message = "C#的事件如何實現的?";  
  15.             Guests[] gArray = new Guests[3]  
  16.             {  
  17.                 new GuestA(){Name = "張小三"},  
  18.                 new GuestB(){Name = "李小四"},  
  19.                 new GuestC(){Name = "王老五"}  
  20.             };  
  21.             //用+=號,將嘉賓的答題方法加入到委托鏈  
  22.             host.QuestionEvent += new QuestionHandler(gArray[0].answer);  
  23.             host.QuestionEvent += new QuestionHandler(gArray[1].answer);  
  24.             host.QuestionEvent += new QuestionHandler(gArray[2].answer);  
  25.   
  26.             //觸發事件  
  27.             host.StartAnswer();  
  28.             Console.ReadLine();  
  29.         }  
  30.     }  
  31. }<span style="color:#ff0000;">  
  32. </span>  



 

 

 

 

Host.cs

 

view plaincopy to clipboardprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     public delegate void QuestionHandler(object sender,QuestionArgs e);  
  9.     public class Host  
  10.     {  
  11.         //定義一個事件   
  12.         public event QuestionHandler QuestionEvent;  
  13.         public QuestionArgs args { set; get; }  
  14.         public Host()  
  15.         {  
  16.             //初始化事件參數   
  17.             args = new QuestionArgs();  
  18.         }  
  19.         public string Name { get; set; }  
  20.         public void StartAnswer()  
  21.         {  
  22.             Console.WriteLine("開始答題");  
  23.             QuestionEvent(this, args);  
  24.         }  
  25.     }  
  26. }  
[csharp] view plaincopyprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     public delegate void QuestionHandler(object sender,QuestionArgs e);  
  9.     public class Host  
  10.     {  
  11.         //定義一個事件  
  12.         public event QuestionHandler QuestionEvent;  
  13.         public QuestionArgs args { set; get; }  
  14.         public Host()  
  15.         {  
  16.             //初始化事件參數  
  17.             args = new QuestionArgs();  
  18.         }  
  19.         public string Name { get; set; }  
  20.         public void StartAnswer()  
  21.         {  
  22.             Console.WriteLine("開始答題");  
  23.             QuestionEvent(this, args);  
  24.         }  
  25.     }  
  26. }  



 

 

 

 

Guests.cs

 

view plaincopy to clipboardprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     /// <summary>   
  9.     /// 父類   
  10.     /// </summary>   
  11.     public class Guests  
  12.     {  
  13.         /// <summary>   
  14.         /// 嘉賓姓名   
  15.         /// </summary>   
  16.         public string Name { get; set; }  
  17.   
  18.         public virtual void answer(object sender, QuestionArgs e)  
  19.         {  
  20.             Console.Write("事件的發出者:" + (sender as Host).Name);  
  21.             Console.WriteLine("問題是:" + e.Message);  
  22.         }  
  23.     }  
  24. }  
[csharp] view plaincopyprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     /// <summary>  
  9.     /// 父類  
  10.     /// </summary>  
  11.     public class Guests  
  12.     {  
  13.         /// <summary>  
  14.         /// 嘉賓姓名  
  15.         /// </summary>  
  16.         public string Name { get; set; }  
  17.   
  18.         public virtual void answer(object sender, QuestionArgs e)  
  19.         {  
  20.             Console.Write("事件的發出者:" + (sender as Host).Name);  
  21.             Console.WriteLine("問題是:" + e.Message);  
  22.         }  
  23.     }  
  24. }  



 

 

 

 

GuestC.cs

 

view plaincopy to clipboardprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     class GuestC:Guests  
  9.     {  
  10.         public override void answer(object sender, QuestionArgs e)  
  11.         {  
  12.             base.answer(sender, e);  
  13.             Console.WriteLine("{0}開始答題:我不知道", this.Name);  
  14.         }  
  15.     }  
  16. }  
[csharp] view plaincopyprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     class GuestC:Guests  
  9.     {  
  10.         public override void answer(object sender, QuestionArgs e)  
  11.         {  
  12.             base.answer(sender, e);  
  13.             Console.WriteLine("{0}開始答題:我不知道", this.Name);  
  14.         }  
  15.     }  
  16. }  



 

 

 

 

GuestB.cs

 

view plaincopy to clipboardprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     class GuestB:Guests  
  9.     {  
  10.         public override void answer(object sender, QuestionArgs e)  
  11.         {  
  12.             base.answer(sender, e);  
  13.             Console.WriteLine("{0}開始答題:我不知道", this.Name);  
  14.         }  
  15.     }  
  16. }  
[csharp] view plaincopyprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     class GuestB:Guests  
  9.     {  
  10.         public override void answer(object sender, QuestionArgs e)  
  11.         {  
  12.             base.answer(sender, e);  
  13.             Console.WriteLine("{0}開始答題:我不知道", this.Name);  
  14.         }  
  15.     }  
  16. }  



 

 

 

 

GuestA.cs

 

view plaincopy to clipboardprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     class GuestA:Guests  
  9.     {  
  10.         public override void answer(object sender, QuestionArgs e)  
  11.         {  
  12.             base.answer(sender, e);  
  13.             Console.WriteLine("{0}開始答題:我不知道", this.Name);  
  14.         }  
  15.     }  
  16. }  
[csharp] view plaincopyprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace EventDemo  
  7. {  
  8.     class GuestA:Guests  
  9.     {  
  10.         public override void answer(object sender, QuestionArgs e)  
  11.         {  
  12.             base.answer(sender, e);  
  13.             Console.WriteLine("{0}開始答題:我不知道", this.Name);  
  14.         }  
  15.     }  
  16. }  

 

 

運行結果:

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新宾| 灯塔市| 雅安市| 崇州市| 剑川县| 民乐县| 景洪市| 麦盖提县| 从江县| 楚雄市| 沙田区| 黎平县| 泸定县| 广饶县| 固原市| 柳林县| 建湖县| 资溪县| 广昌县| 邓州市| 塔河县| 房山区| 遂宁市| 登封市| 大石桥市| 灯塔市| 济南市| 蓝山县| 库尔勒市| 虹口区| 潜山县| 应城市| 西乡县| 临泽县| 增城市| 额济纳旗| 定兴县| 平江县| 阳原县| 宿迁市| 芜湖县|