第3章 C#中的委托和事件
.NET框架中的委托和事件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Text.RegularExpressions; namespace _.net之美
{
class Program
{
static void Main(string[] args)
{
Heater heater = new Heater();
Alert alert = new Alert();
heater.boiled += alert.AlertTemperature;
heater.boiled += Display.ShowMsg;
heater.BoilWater();
Console.ReadKey();
}
} public class Heater
{
private readonly string area="ChengDu";
public string Area { get { return area; } } private readonly string type = "";
public string Type { get { return type; } } private int temperature; public delegate void BoiledEventHandle(object sender, BoiledEventArgs args);
public event BoiledEventHandle boiled; public void BoilWater()
{
for (int i = ; i <= ; i++)
{
temperature = i;
BoiledEventArgs e = new BoiledEventArgs(temperature);
if (temperature > )
{
OnBoiled(e);
}
}
} private void OnBoiled(BoiledEventArgs e)
{
if (boiled != null)
boiled(this, e);
} public class BoiledEventArgs
{
private readonly int temperature;
public BoiledEventArgs(int temperature)
{
this.temperature = temperature;
} public int Temperature { get { return this.temperature; } }
}
} public class Alert
{
public void AlertTemperature(object sender, Heater.BoiledEventArgs e)
{
Heater heater = (Heater)sender;
Console.WriteLine("Area:{0},Type:{1}", heater.Area, heater.Type);
Console.WriteLine("水快烧开了!");
}
} public class Display
{
public static void ShowMsg(object sender, Heater.BoiledEventArgs e)
{
Heater heater = (Heater)sender;
Console.WriteLine("Area:{0},Type:{1}", heater.Area, heater.Type);
Console.WriteLine("当前温度:{0}", e.Temperature);
}
}
}
委托进阶
第3章 C#中的委托和事件的更多相关文章
- 【转载】C# 中的委托和事件(详解:简单易懂的讲解)
本文转载自http://www.cnblogs.com/SkySoot/archive/2012/04/05/2433639.html C# 中的委托和事件(详解) C# 中的委托和事件 委托和事件在 ...
- C# 中的委托和事件(详解)
C# 中的委托和事件 委托和事件在 .NET Framework 中的应用非常广泛,然而,较好地理解委托和事件对很多接触 C# 时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真是太 ...
- 【转载】C# 中的委托和事件(详解)
<div class="postbody"> <div id="cnblogs_post_body" class="blogpost ...
- C# 中的委托和事件
觉得这篇文章写的非常好,大神之作,由简入繁,对我这种初学者来说帮忙很大,特此留存下. 摘自:http://tracefact.net/CSharp-Programming/Delegates-and- ...
- C# 中的委托和事件(转)
引言 委托 和 事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真是太容易了,而没有过去 ...
- C# 中的委托和事件(转载)
引言 委托 和 事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真是太容易了,而没有过去 ...
- 【转】C# 中的委托和事件
阅读目录 C# 中的委托和事件 引言 将方法作为方法的参数 将方法绑定到委托 事件的由来 事件和委托的编译代码 委托.事件与Observer设计模式 .Net Framework中的委托与事件 总结 ...
- 分分钟用上C#中的委托和事件之窗体篇
上次以鸿门宴的例子写了一篇名为<分分钟用上C#中的委托和事件>的博文,旨在帮助C#初学者迈过委托和事件这道坎,能够用最快的速度掌握如何使用它们.如果觉得意犹未尽,或者仍然不知如何在实际应用 ...
- 《C#高级编程》学习笔记------C#中的委托和事件(续)
本文转载自张子阳 目录 为什么要使用事件而不是委托变量? 为什么委托定义的返回值通常都为void? 如何让事件只允许一个客户订阅?(事件访问器) 获得多个返回值与异常处理 委托中订阅者方法超时的处理 ...
随机推荐
- MFC CPtrLink的使用
if (!m_SALink.IsEmpty()) { POSITION pos = m_SALink.GetHeadPosition(); for (int j = 0; j < m_SALin ...
- linux keepalived+LVS 实现mysql 从库负载均衡
前情提要: 参考链接: http://www.osyunwei.com/archives/7464.html ps:以上为本次操作的主要参考资料,非常感谢此文作者的贡献,我的随笔的主要目的是 说明在使 ...
- 获取父iframe的高宽
var p_window = window.top; //alert($(p_window).height()); var p_demo = window.top.docu ...
- 【uTenux实验】时间管理(系统时间/周期性处理/警报处理)
1.系统时间管理 系统时间管理函数用来对系统时间进行操作,是OS的一个基础性的东西.个人认为,设置系统时间和获取系统时间对OS来说基本是可有可无的. uTenux提供了三个系统时间相关API.分别用于 ...
- iis 应用程序池看不到 .net framework 4.0
我的情况是,先配置了iis,然后再安装.net framework 4.0 进去设置应用程序池的时候,没有找到 .net framework 4.0 ,经过一番尝试,无效,最后无奈重启. 好了.
- Js 扩展
计算字符串的字节长度 String.prototype.len = function () { return this.replace(/[^\x00-\xff]/g, 'xx').length; } ...
- rpm包形式安装MySQL
1.下载Mysql安装所需的rpm包 http://cdn.mysql.com/Downloads/MySQL-5.6/MySQL-server-5.6.21-1.linux_glibc2.5.x86 ...
- FreeBSD 10 中文环境
中文字体位置 FontPath "/usr/local/lib/X11/fonts/misc/:unscaled" FontPath "/us ...
- 【转】关于LWF——线性工作流
1.什么是LWF? LWF全称Linear Workflow,中文翻译为线性工作流.“工作流”在这里可以当作工作流程来理解.LWF就是一种通过调整图像Gamma值,来使得图像得到线性化显示的技术流程. ...
- 【转】提高VR渲染速度的最好方法(经典转载)
VR的基本渲染方法掌握起来并不难,但是最迫切需要解决的问题是VR的出图速度问题.动则需要数小时的渲染时间真的是很难以接受,我们从三个影响速度的参数结合网上一些高手的教程来分析一下. 一.Irradia ...