C# 委托现实范例代码
发布时间:2020-12-15 17:55:29 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 using System;using System.Collections.Generic;using System.Text;namespace Delegate { // 热水器 public class Heater { private int temperature
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 using System; using System.Collections.Generic; using System.Text; namespace Delegate { // 热水器 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 > 95) { 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:水快烧开了,当前温度:{0}度。",param); } } class Program { static void Main() { Heater heater = new Heater(); Alarm alarm = new Alarm(); heater.BoilEvent += alarm.MakeAlert; //注册方法 heater.BoilEvent += (new Alarm()).MakeAlert; //给匿名对象注册方法 heater.BoilEvent += Display.ShowMsg; //注册静态方法 heater.BoilWater(); //烧水,会自动调用注册过对象的方法 } } } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- flex使用filereference+httphandler实现文件上传/下载
- 第二章:Cocos2d-x中的基础概念
- EasyRTMP手机直播推送rtmp流flash无法正常播放问题
- ruby-on-rails – Rails 5.1.2 – form_with在html中没有显
- 编写react组件最佳实践
- 文档在线预览(三)- 使用SWFTools转换文档为SWF格式
- 在C#中返回字符的代码点
- 真实场景下Oracle Sharding的优势比较和选择
- Cocos2d-x3.3Final(1) ListView常用成员函数(C++)
- xcode – Cocoapods – Flurry&TestFlight – 未定义的