C#知识点:委托、事件、正则表达式、SVN、找按段等差递增至不变序列的规律
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Delegate {
- //定义委托,它定义了可以代表的方法的类型
- public delegate void GreetingDelegate(string Greet ,string name);
- class Program {
- private static void Greeting(string Greet , string name) {
- Console.WriteLine(Greet+", " + name);
- }
- //注意此方法,它接受一个GreetingDelegate类型的方法作为参数
- private static void GreetPeople(string Greet , string name, GreetingDelegate MakeGreeting) {
- MakeGreeting(Greet , name);
- }
- static void Main(string[] args) {
- GreetPeople("Morning","Jimmy Zhang", Greeting);
- GreetPeople("早上好","张子阳", Greeting);
- Console.ReadKey();
- }
- }
- }
- 输出如下:
- Morning, Jimmy Zhang
- 早上好, 张子阳
委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法,可以避免在程序中大量使用If-Else(Switch)语句,同时使得程序具有更好的可扩展性。
既然给委托可以绑定一个方法,那么也应该有办法取消对方法的绑定,很容易想到,这个语法是“-=”:
使用委托可以将多个方法绑定到同一个委托变量,当调用此变量时(这里用“调用”这个词,是因为此变量代表一个方法),可以依次调用所有绑定的方法。
事件其实没什么不好理解的,声明一个事件不过类似于声明一个进行了封装的委托类型的变量而已。
Observer设计模式:Observer设计模式是为了定义对象间的一种一对多的依赖关系,以便于当一个对象的状态改变时,其他依赖于它的对象会被自动告知并更新。Observer模式是一种松耦合的设计模式
- 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(); //烧水,会自动调用注册过对象的方法
- }
- }
- }
- 输出为:
- Alarm:嘀嘀嘀,水已经 96 度了:
- Alarm:嘀嘀嘀,水已经 96 度了:
- Display:水快烧开了,当前温度:96度。
- // 省略...
.Net Framework的编码规范:
- 委托类型的名称都应该以EventHandler结束。
- 委托的原型定义:有一个void返回值,并接受两个输入参数:一个Object 类型,一个 EventArgs类型(或继承自EventArgs)。
- 事件的命名为 委托去掉 EventHandler之后剩余的部分。
- 继承自EventArgs的类型应该以EventArgs结尾。
再做一下说明:
- 委托声明原型中的Object类型的参数代表了Subject,也就是监视对象,在本例中是 Heater(热水器)。回调函数(比如Alarm的MakeAlert)可以通过它访问触发事件的对象(Heater)。
- EventArgs 对象包含了Observer所感兴趣的数据,在本例中是temperature。
上面这些其实不仅仅是为了编码规范而已,这样也使得程序有更大的灵活性。比如说,如果我们不光想获得热水器的温度,还想在Observer端(警报器或者显示器)方法中获得它的生产日期、型号、价格,那么委托和方法的声明都会变得很麻烦,而如果我们将热水器的引用传给警报器的方法,就可以在方法中直接访问热水器了。
改写之前的范例,让它符合 .Net Framework 的规范:
- 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 = ; i <= ; i++) {
- temperature = i;
- if (temperature > ) {
- //建立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 :
- Alarm: 嘀嘀嘀,水已经 度了:
- Alarm:China Xian - RealFire :
- Alarm: 嘀嘀嘀,水已经 度了:
- Alarm:China Xian - RealFire :
- Alarm: 嘀嘀嘀,水已经 度了:
- Display:China Xian - RealFire :
- Display:水快烧开了,当前温度:96度。
- // 省略 ...
参考:http://www.tracefact.net/CSharp-Programming/Delegates-and-Events-in-CSharp.aspx
正则表达式:
IP地址(Regex.IsMatch(strIn,@ "^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$ ");)
参考:http://www.cnblogs.com/miniwiki/archive/2010/06/08/1754269.html#undefined
参考:http://deerchao.net/tutorials/regex/regex.htm
- SVN:http://blog.csdn.net/qing_gee/article/details/47341381
- 找按段等差递增至不变序列的规律:
- try
- {
- if ((UnitPrice <= ) || (Unit <= ))
- {
- double[] arrPrice = new double[];
- double[,] arrNum = new double[, ];
- for (int i = ; i < ; i++)
- arrPrice[i] = (double)row["Price" + i.ToString()];
- for (int j = ; j < ; j++)
- {
- for (int i = ; (i + j+j ) < ; i++)
- {
- arrNum[j, i+] = arrPrice[i + j+j] - arrPrice[i + j];
- }
- }
- for (int i = ; i < arrNum.GetLength(); i++)
- {
- int i1 = arrNum.GetLength();//
- int i2 = arrNum.GetLength();//
- int Icount1 = ;
- int Icount2 = ;
- int Icount3 = ;
- double unitPrice = ;
- for (int j = ; j < (arrNum.GetLength()-); j++)
- {
- if ((arrNum[i, j] == ) && (Icount2==))
- {
- Icount1++;
- }
- else if ((arrNum[i, j] == arrNum[i, j + ]) && (arrNum[i, j + ] == arrNum[i, j + + ]) && (arrNum[i, j ] != ))
- {
- Icount2++;
- unitPrice=arrNum[i, j];
- }
- else if ((arrNum[i, j+] == ) && (arrNum[i, j + +] == ))
- {
- Icount3++;
- }
- }
- if ((Icount1 + Icount2 + Icount3) == ( - -))
- {
- UnitPrice = unitPrice;
- Unit = * i;
- break;
- }
- }
- }
- }
- catch (Exception ex)
- {
- }
C#知识点:委托、事件、正则表达式、SVN、找按段等差递增至不变序列的规律的更多相关文章
- 8.C#知识点:委托和事件
知识点目录==========>传送门 首先推荐两篇大牛写的委托和事件的博客,写的超级好!看了就包你看会,想学习的朋友直接看这两篇就足以,我自己写的是算是自己学习的纪录. 传送门======== ...
- Observer设计模式中-委托事件-应用在消息在窗体上显示
Observer设计模式:监视者模式.在类中的方法中处理的结果或者消息通过事件委托 的方式发送给主窗体. 因为在其它类中直接访问主窗体类,显示内容是不能直接调用控件赋值的,当然也有别的类似查阅控件名, ...
- python 全栈开发,Day55(jQuery的位置信息,JS的事件流的概念(重点),事件对象,jQuery的事件绑定和解绑,事件委托(事件代理))
一.jQuery的位置信息 jQuery的位置信息跟JS的client系列.offset系列.scroll系列封装好的一些简便api. 一.宽度和高度 获取宽度 .width() 描述:为匹配的元素集 ...
- 委托事件(jQuery)
<div class="content"> <ul> <li>1</li> <li>2</li> <l ...
- C# ~ 从 委托事件 到 观察者模式 - Observer
委托和事件的部分基础知识可参见 C#/.NET 基础学习 之 [委托-事件] 部分: 参考 [1]. 初识事件 到 自定义事件: [2]. 从类型不安全的委托 到 类型安全的事件: [3]. 函数指针 ...
- C#委托,事件理解入门 (译稿)
原文地址:http://www.codeproject.com/Articles/4773/Events-and-Delegates-Simplified 引用翻译地址:http://www.cnbl ...
- 关于ios使用jquery的on,委托事件失效
$('.parents').on("click",'.child',function(){}); 类似上面这种,在ios上点击"child"元素不会起作用,解决 ...
- Asp.net用户控件和委托事件
在Asp.net系统制作过程中,门户类型的网站,我们可以用DIV+CSS+JS+Ajax全部搞定,但是一旦遇到界面元素比较复杂的时候,还是UserControl比较方便一些,各种封装,各种处理,然后拖 ...
- jQuery里面的普通绑定事件和on委托事件
以click事件为例: 普通绑定事件:$('.btn1').click(function(){}绑定 on绑定事件:$(document).on('click','.btn2',function(){ ...
随机推荐
- 【MyBatis】----【MyBatis】--封装---别名---properties
一.核心配置文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration ...
- HTML5实现绘制几何图形
HTML5新增了一个<canvas.../>属性.该元素自身并不绘制图形,只是相当于一张空画布.如果开发者需要向<canvas.../>上绘制图形则必须使用JavaScript ...
- HTTPS测试
1.首先理解HTTPS的含义,清楚http与HTTPS的区别 2.相对于http而言,https更加安全,因 3.使用数字证书使传输更安全,数字证书使用keytool工具生成 测试准备: 创建公钥和秘 ...
- kafka producer发送消息 Failed to update metadata after问题
提示示例: ERROR Error when sending message to topic test with key: null, value: 2 bytes with error: Fail ...
- 第六周总结&第四次实验报告
实验四 类的继承 一. 实验目的 (1) 掌握类的继承方法: (2) 变量的继承和覆盖,方法的继承.重载和覆盖实现: 二. 实验内容 三.实验过程 实验代码 package Shiyan4; publ ...
- webapi接口统一返回请求时间
webapi接口统一返回请求时间: public class BaseController : ControllerBase { protected ReturnResult<T> Res ...
- dfs(最佳路径)
http://acm.hdu.edu.cn/showproblem.php?pid=1242 Rescue Time Limit: 2000/1000 MS (Java/Others) Memo ...
- nodejs版实现properties后缀文件解析
1.propertiesParser.js let readline = require('readline'); let fs = require('fs'); // properties文件路径 ...
- 100行代码撸完SpringIOC容器
用过Spring框架的人一定都知道Spring的依赖注入控制反转;通俗的讲就是负责实例化对象 和 管理对象间的依赖 实现解耦. 我们来对比两段代码: UserController{ UserServi ...
- 从零开始配置安装Flutter开发环境
flutter 中文网 https://flutterchina.club/get-started/install/ 1.配置全局环境 PUB_HOSTED_URL=https://pub.flutt ...