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(){ ...
随机推荐
- Tomcat使用介绍
一.tomcat介绍 Tomcat服务器是一个免费的开放源代码的轻量级Web 应用服务器,如apache处理静态HTML能力突出不同,tomcat处理动态HTML能力相当强大,因此一般项目都是部署ap ...
- Delphi下利用WinIo模拟鼠标键盘详解 有参考价值
https://blog.csdn.net/fgrass_163/article/details/6365296 Delphi下利用WinIo模拟鼠标键盘详解 2011年04月26日 21:03:00 ...
- Flink数据流图的生成----简单执行计划的生成
Flink的数据流图的生成主要分为简单执行计划-->StreamGraph的生成-->JobGraph的生成-->ExecutionGraph的生成-->物理执行图.其中前三个 ...
- web form 防止一个请求重复提交
/// <summary> /// 防止一个请求重复提交 /// </summary> public void PreventRepeatSubmit() { if (Scri ...
- python+selenium控制浏览器窗口(刷新、前进、后退、退出浏览器)
调用说明: driver.属性值 变量说明: 1.driver.current_url:用于获得当前页面的URL 2.driver.title:用于获取当前页面的标题 3.driver.page_so ...
- express中app.use()使用方法
app.use([path,] function [, function…]) 在path上安装中间件,如果path没有被设定,那么默认为”/”. 当为路由设置一个匹配路径后,路由会匹配该路径及该路径 ...
- Java实验报告(一)
1.水仙花数 public class test1{ public static void main(String[] args){ for(int num=100;num<1000;num++ ...
- mysql日志信息查看与设置mysql-bin
查看 sql查询记录 日志是否开启 SHOW GLOBAL VARIABLES LIKE '%general_log%' 二进制日志 是否开启 SHOW GLOBAL VARIABLES LIKE ...
- tensorflow白话篇
接触机器学习也有相当长的时间了,对各种学习算法都有了一定的了解,一直都不愿意写博客(借口是没时间啊),最近准备学习深度学习框架tensorflow,决定还是应该把自己的学习一步一步的记下来,方便后期的 ...
- promise和async/await的用法
promise和async都是做异步处理的, 使异步转为同步 1.promise 它和Promise诞生的目的都是为了解决“回调地狱”, promise使用方法: <button @click= ...