Delegate学习笔记
生活随笔
收集整理的這篇文章主要介紹了
Delegate学习笔记
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
代碼 using?System;
using?System.Collections.Generic;
using?System.Text;
namespace?Delegate?{
????//?熱水器
????public?class?Heater?{
???????private?int?temperature;
???????public?string?type?=?"RealFire?001";???????//?添加型號作為演示
???????public?string?area?=?"China?Xian";?????????//?添加產地作為演示
???????//聲明委托
???????public?delegate?void?BoiledEventHandler(Object?sender,?BoiledEventArgs?e);
???????public?event?BoiledEventHandler?Boiled;?//聲明事件
???????//?定義BoiledEventArgs類,傳遞給Observer所感興趣的信息
???????public?class?BoiledEventArgs?:?EventArgs?{
???????????public?readonly?int?temperature;
???????????public?BoiledEventArgs(int?temperature)?{
??????????????this.temperature?=?temperature;
???????????}
???????}
???????//?可以供繼承自?Heater?的類重寫,以便繼承類拒絕其他對象對它的監視
???????protected?virtual?void?OnBoiled(BoiledEventArgs?e)?{
???????????if?(Boiled?!=?null)?{?//?如果有對象注冊
??????????????Boiled(this,?e);??//?調用所有注冊對象的方法
???????????}
???????}
???????
???????//?燒水。
???????public?void?BoilWater()?{
???????????for?(int?i?=?0;?i?<=?100;?i++)?{
??????????????temperature?=?i;
??????????????if?(temperature?>?95)?{
??????????????????//建立BoiledEventArgs?對象。
??????????????????BoiledEventArgs?e?=?new?BoiledEventArgs(temperature);
??????????????????OnBoiled(e);??//?調用?OnBolied方法
??????????????}
???????????}
???????}
????}
????//?警報器
????public?class?Alarm?{
???????public?void?MakeAlert(Object?sender,?Heater.BoiledEventArgs?e)?{
???????????Heater?heater?=?(Heater)sender;?????//這里是不是很熟悉呢?
???????????//訪問?sender?中的公共字段
???????????Console.WriteLine("Alarm:{0}?-?{1}:?",?heater.area,?heater.type);
???????????Console.WriteLine("Alarm:?嘀嘀嘀,水已經?{0}?度了:",?e.temperature);
???????????Console.WriteLine();
???????}
????}
????//?顯示器
????public?class?Display?{
???????public?static?void?ShowMsg(Object?sender,?Heater.BoiledEventArgs?e)?{???//靜態方法
???????????Heater?heater?=?(Heater)sender;
???????????Console.WriteLine("Display:{0}?-?{1}:?",?heater.area,?heater.type);
???????????Console.WriteLine("Display:水快燒開了,當前溫度:{0}度。",?e.temperature);
???????????Console.WriteLine();
???????}
????}
????class?Program?{
???????static?void?Main()?{
???????????Heater?heater?=?new?Heater();
???????????Alarm?alarm?=?new?Alarm();
???????????heater.Boiled?+=?alarm.MakeAlert;???//注冊方法
???????????heater.Boiled?+=?(new?Alarm()).MakeAlert;??????//給匿名對象注冊方法
???????????heater.Boiled?+=?new?Heater.BoiledEventHandler(alarm.MakeAlert);????//也可以這么注冊
???????????heater.Boiled?+=?Display.ShowMsg;???????//注冊靜態方法
???????????heater.BoilWater();???//燒水,會自動調用注冊過對象的方法
???????}
????}
}
輸出為:
Alarm:China?Xian?-?RealFire?001:
Alarm:?嘀嘀嘀,水已經?96?度了:
Alarm:China?Xian?-?RealFire?001:
Alarm:?嘀嘀嘀,水已經?96?度了:
Alarm:China?Xian?-?RealFire?001:
Alarm:?嘀嘀嘀,水已經?96?度了:
Display:China?Xian?-?RealFire?001:
Display:水快燒開了,當前溫度:96度。
//?省略?...
using?System.Collections.Generic;
using?System.Text;
namespace?Delegate?{
????//?熱水器
????public?class?Heater?{
???????private?int?temperature;
???????public?string?type?=?"RealFire?001";???????//?添加型號作為演示
???????public?string?area?=?"China?Xian";?????????//?添加產地作為演示
???????//聲明委托
???????public?delegate?void?BoiledEventHandler(Object?sender,?BoiledEventArgs?e);
???????public?event?BoiledEventHandler?Boiled;?//聲明事件
???????//?定義BoiledEventArgs類,傳遞給Observer所感興趣的信息
???????public?class?BoiledEventArgs?:?EventArgs?{
???????????public?readonly?int?temperature;
???????????public?BoiledEventArgs(int?temperature)?{
??????????????this.temperature?=?temperature;
???????????}
???????}
???????//?可以供繼承自?Heater?的類重寫,以便繼承類拒絕其他對象對它的監視
???????protected?virtual?void?OnBoiled(BoiledEventArgs?e)?{
???????????if?(Boiled?!=?null)?{?//?如果有對象注冊
??????????????Boiled(this,?e);??//?調用所有注冊對象的方法
???????????}
???????}
???????
???????//?燒水。
???????public?void?BoilWater()?{
???????????for?(int?i?=?0;?i?<=?100;?i++)?{
??????????????temperature?=?i;
??????????????if?(temperature?>?95)?{
??????????????????//建立BoiledEventArgs?對象。
??????????????????BoiledEventArgs?e?=?new?BoiledEventArgs(temperature);
??????????????????OnBoiled(e);??//?調用?OnBolied方法
??????????????}
???????????}
???????}
????}
????//?警報器
????public?class?Alarm?{
???????public?void?MakeAlert(Object?sender,?Heater.BoiledEventArgs?e)?{
???????????Heater?heater?=?(Heater)sender;?????//這里是不是很熟悉呢?
???????????//訪問?sender?中的公共字段
???????????Console.WriteLine("Alarm:{0}?-?{1}:?",?heater.area,?heater.type);
???????????Console.WriteLine("Alarm:?嘀嘀嘀,水已經?{0}?度了:",?e.temperature);
???????????Console.WriteLine();
???????}
????}
????//?顯示器
????public?class?Display?{
???????public?static?void?ShowMsg(Object?sender,?Heater.BoiledEventArgs?e)?{???//靜態方法
???????????Heater?heater?=?(Heater)sender;
???????????Console.WriteLine("Display:{0}?-?{1}:?",?heater.area,?heater.type);
???????????Console.WriteLine("Display:水快燒開了,當前溫度:{0}度。",?e.temperature);
???????????Console.WriteLine();
???????}
????}
????class?Program?{
???????static?void?Main()?{
???????????Heater?heater?=?new?Heater();
???????????Alarm?alarm?=?new?Alarm();
???????????heater.Boiled?+=?alarm.MakeAlert;???//注冊方法
???????????heater.Boiled?+=?(new?Alarm()).MakeAlert;??????//給匿名對象注冊方法
???????????heater.Boiled?+=?new?Heater.BoiledEventHandler(alarm.MakeAlert);????//也可以這么注冊
???????????heater.Boiled?+=?Display.ShowMsg;???????//注冊靜態方法
???????????heater.BoilWater();???//燒水,會自動調用注冊過對象的方法
???????}
????}
}
輸出為:
Alarm:China?Xian?-?RealFire?001:
Alarm:?嘀嘀嘀,水已經?96?度了:
Alarm:China?Xian?-?RealFire?001:
Alarm:?嘀嘀嘀,水已經?96?度了:
Alarm:China?Xian?-?RealFire?001:
Alarm:?嘀嘀嘀,水已經?96?度了:
Display:China?Xian?-?RealFire?001:
Display:水快燒開了,當前溫度:96度。
//?省略?...
轉自:http://www.cnblogs.com/JimmyZhang/archive/2007/09/23/903360.html
轉載于:https://www.cnblogs.com/johnwonder/archive/2010/02/25/1673843.html
總結
以上是生活随笔為你收集整理的Delegate学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 12种性格导致贫穷
- 下一篇: sql server:删除表数据,标识列