创建一个应用台控制应用程序,假设设计一个高档热水器,当通电加热到时温度超过96时,扬声器发出声音告诉用户谁的温度,液晶屏显示水温的变化表示水快开了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace HeaterEvent { public class Heater { private int temperature; public delegate void BoilHandler(int param); public event BoilHandler BoilEvent; public void BoilWater() { for(int i = 0; i <= 100; i++) { temperature = i; if(temperature>96) { if(BoilEvent!=null) { BoilEvent(temperature);
} } } } } public class Alarm { public void MakeAlert(int param) { Console.WriteLine("Alarm:嘀嘀嘀,水已经{0}度了", param); } } public class Display { public static void ShowMsg(int param) { Console.WriteLine("Display:水快烧开了,当前温度:", param);
} } class Program { static void Main(string[] args) { Heater ht = new Heater(); Alarm al = new Alarm(); ht.BoilEvent += al.MakeAlert; ht.BoilEvent += Display.ShowMsg; ht.BoilWater(); Console.ReadKey();
} } }
|
