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(){ ...
随机推荐
- 理解ES6中的Symbol
一.为什么ES6引入Symbol 有时候我们在项目开发的过程中可能会遇到这样的问题,我写了一个对象,而另外的同时则在这个对象里面添加了一个属性或是方法,倘若添加的这个属性或是方法是原本的对象中本来就有 ...
- 【SpringMVC】---搭建框架步骤
项目如下 一.加入 Jar 包 部分jar包可以不导(第4.9.11个可以不导入) 二.在 Web.xml 中配置 DispatcherServlet <?xml version="1 ...
- python使用相对路径创建文件夹
两个py文件,一个是al文件夹下的test1.py,一个是和al文件夹同层的test2.py test1.py代码如下: import os def test(): path = './source_ ...
- Java ——泛型 序列化
本节重点思维导图 泛型 序列化 泛型 import java.util.ArrayList; import java.util.Date; import java.util.Iterator; i ...
- DedeCMS调取其他织梦CMS站点数据库数据方法
第1步:打开网站include\taglib文件夹中找到sql.lib.php文件,并直接复制一些此文件出来,并把复制出来的这个文件重命名为mysql.lib.php.注:mysql.lib.php, ...
- 【HBase】三、HBase和RDBMS的比较
HBase作为一种NoSQL的数据库,和传统的类似于mysql这样的关系型数据库是有很大区别的,本文来对他们做一个对比分析,以便更加深入的了解HBase. 主要区别体现在以下六个方面: 1 ...
- 学了一天的golang从入门到放弃
Google的go就是个二货,不实用,它最多只能和c比简单.low!
- Springboot2.x集成单节点Redis
Springboot2.x集成单节点Redis 说明 在Springboot 1.x版本中,默认使用Jedis客户端来操作Redis,而在Springboot 2.x 版本中,默认使用Lettuce客 ...
- 分享一个linux中测试网站是否正常的shell脚本
#! /bin/bash #Author=Harry CheckUrl() { #<==定义函数,名字为CheckUrl timeout=5 #<==定义wget访问的超时时间,超时就退出 ...
- Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cli2/Option
今天,在做canopy算法实例时,遇到这个问题,所以记录下来.下面是源码: package czx.com.mahout; import java.io.IOException; import org ...