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(){ ...
随机推荐
- java日常统计
姓名:Danny 日期:2017/11/27 任务 日期 听课 编程程序 阅读课本 准备考试 日统计 周一 30 120 150 周二 50 140 190 周三 周四 周五 周六 周 ...
- python2与3版本的编码问题
python的str默认是ascii编码,和unicode编码冲突,就会报这个标题错误:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 ...
- 【JZOJ 3909】Idiot 的乘幂
题面: 正文: 把题目中的方程组组合在一起就变成了: \(X^{a+c}\equiv b \cdot d (\mod p)\) 那这时,我们假定两个数\(x\)和\(y\),使得: \(ax + cy ...
- package.json的所有配置项及其用法,你都熟悉么
写在前面 在前端开发中,npm已经是必不可少的工具了.使用npm,不可避免的就要和package.json打交道.平时package.json用得挺多,但是没有认真看过官方文档.本文结合npm官方文档 ...
- zprofiler工具
转自:zprofiler三板斧解决cpu占用率过高问题 此工具为阿里自产的profiler工具,在其他文章中看到有用此工具进行性能问题定位的.在此转载文章学习一下. 上周五碰到了一个线上机器cpu占用 ...
- 分布式事务——幂等设计(rocketmq案例)
幂等指的就是执行多次和执行一次的效果相同,主要是为了防止数据重复消费.MQ中为了保证消息的可靠性,生产者发送消息失败(例如网络超时)会触发 "重试机制",它不是生产者重试而是MQ自 ...
- html5移动端Meta的设置
强制让文档的宽度与设备的宽度保持1:1,并且文档最大的宽度比例是1.0,且不允许用户点击屏幕放大浏览 1 <meta name="viewport" content=&quo ...
- 广播即时通信src和des
package 第十二章; import java.io.IOException; import java.net.DatagramPacket; import java.net.InetAddres ...
- Vue准备
Vue 模板 <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UT ...
- 1、Framework7
一. <!DOCTYPE html> <html> <head> <!-- 所需的Meta标签--> <meta charset="ut ...