c#委托delegate简单例子

Code:
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Linq;   
  4. using System.Text;   
  5.   
  6. namespace Delegate   
  7. {   
  8.     //热水器   
  9.     public class Heater   
  10.     {   
  11.         private int temperature;//水温   
  12.         public string type = "realfire 007";//型号   
  13.         public string area = "China Xian";//产地   
  14.         public delegate void BoilEventHandler(Object sender, BoilEventArgs e);//声明委托   
  15.         public event BoilEventHandler Boiled;//声明事件   
  16.         //定义BoilEventArgs类,传递给Observer所感兴趣的信息   
  17.         public class BoilEventArgs : EventArgs   
  18.         {   
  19.             public readonly int temperature;   
  20.             public BoilEventArgs(int temperature)   
  21.             {   
  22.                 this.temperature = temperature;   
  23.             }   
  24.         }   
  25.         //可以供继承自heater的类重写,以便继承类拒绝其他对象对它的监视   
  26.         protected virtual void OnBoiled(BoilEventArgs e)   
  27.         {   
  28.             if (Boiled != null)   
  29.                 Boiled(this, e);   
  30.         }   
  31.         //烧水   
  32.         public void BoilWate()   
  33.         {   
  34.             for (int i = 0; i <= 100; i++)   
  35.             {   
  36.                 temperature = i;   
  37.                 if (temperature > 95)   
  38.                 {   
  39.                     BoilEventArgs e = new BoilEventArgs(temperature);   
  40.                     OnBoiled(e);   
  41.                 }   
  42.             }   
  43.         }   
  44.     }   
  45.     //报警器   
  46.     public class Alarm   
  47.     {   
  48.         //发出嘟嘟嘟的警报声   
  49.         public void MakeAlert(Object sender,Heater.BoilEventArgs e)   
  50.         {   
  51.             Heater heater = (Heater)sender;   
  52.             //访问sender中的公共字段   
  53.             Console.WriteLine("Alarm :{0}-{1}", heater.area,heater.type);   
  54.             Console.WriteLine("Alarm :嘀嘀嘀,水已经{0}度了!", e.temperature);   
  55.         }   
  56.   
  57.     }   
  58.     //显示器   
  59.     public class Display   
  60.     {   
  61.         //显示水温   
  62.         public static void ShowMsg(Object sender,Heater.BoilEventArgs e)   
  63.         {   
  64.             Heater heater = (Heater)sender;   
  65.             //访问sender中的公共字段   
  66.             Console.WriteLine("Display:{0}-{1}", heater.area, heater.type);   
  67.             Console.WriteLine("Display: 水快开了,当前温度:{0}度。",e.temperature);   
  68.         }   
  69.     }   
  70.     class Program   
  71.     {   
  72.         static void Main(string[] args)   
  73.         {   
  74.             Heater ht = new Heater();   
  75.             Alarm alarm = new Alarm();   
  76.             ht.Boiled += alarm.MakeAlert;//注册方法   
  77.             ht.Boiled += (new Alarm()).MakeAlert;//给匿名对象注册方法   
  78.             ht.Boiled += new Heater.BoilEventHandler(alarm.MakeAlert);//也可以这么注册   
  79.             ht.Boiled += Display.ShowMsg;//注册静态方法   
  80.             ht.BoilWate();   
  81.             Console.ReadKey();   
  82.         }   
  83.     }   
  84. }   

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值