4.1 delegate
delegate ---packed up function
public delegate double myDelegate (double x);
my delegate d2 = new myDelegate (obj.method);
using System; delegate double Fun( double x ); public class DelegateIntegral
{
public static void Main()
{
Fun fun = new Fun(Math.Sin);
double d = Integral( fun, , Math.PI/, 1e- );
Console.WriteLine( d ); Fun fun2 = new Fun( Linear );
double d2 = Integral( fun2, , , 1e- );
Console.WriteLine( d2 ); Rnd rnd = new Rnd();
double d3 = Integral( new Fun(rnd.Num), , , 0.01 );
Console.WriteLine( d3 );
} static double Linear( double a )
{
return a*+;
} class Rnd
{
Random r = new Random();
public double Num( double x )
{
return r.NextDouble();
}
} static double Integral(Fun f, double a, double b, double eps)// 积分计算
{
int n,k;
double fa,fb,h,t1,p,s,x,t=; fa=f(a);
fb=f(b); // 迭代初值
n=;
h=b-a;
t1=h*(fa+fb)/2.0;
p=double.MaxValue; // 迭代计算
while (p>=eps)
{
s=0.0;
for (k=;k<=n-;k++)
{
x=a+(k+0.5)*h;
s=s+f(x);
} t=(t1+h*s)/2.0;
p=Math.Abs(t1-t);
t1=t;
n=n+n;
h=h/2.0;
}
return t;
} }
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data; namespace TestWin
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
//
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(, );
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(, );
this.button1.TabIndex = ;
this.button1.Text = "button1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(, );
this.ClientSize = new System.Drawing.Size(, );
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.ResumeLayout(false); }
#endregion /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = Pens.Blue; Fun [] funs = {
new Fun( this.Square ),
new Fun( Form1.XPlus ),
new Fun( Math.Cos ),
new Fun( Math.Sqrt )
};
foreach( Fun fun in funs )
{
PlotFun( fun, g, pen );
}
} delegate double Fun( double x ); void PlotFun( Fun fun, Graphics g, Pen pen )
{
for( double x=; x<; x+=0.1)
{
double y = fun(x);
Point point = new Point( (int)(x*), (int)(-y*) );
g.DrawLine( pen, point, new Point( point.X+, point.Y+) );
Console.WriteLine( " " + x + " " + y );
}
} double Square( double x )
{
return Math.Sqrt( Math.Sqrt( x) );
}
static double XPlus( double x )
{
return Math.Sin(x)+ Math.Cos(x*)/;
} }
}
using System;
delegate void D(int x);
class C
{
public static void M1(int i)
{
Console.WriteLine("C.M1: " + i);
}
public static void M2(int i)
{
Console.WriteLine("C.M2: " + i);
}
public void M3(int i)
{
Console.WriteLine("C.M3: " + i);
}
}
class Test
{
static void Main()
{
D cd1 = new D( C.M1);
cd1(-); // call M1
D cd2 = null;
cd2 += new D( C.M2);
cd2(-); // call M2
D cd3 = cd1 + cd2;
cd3(); // call M1 then M2
cd3 += cd1;
cd3(); // call M1, M2, then M1
C c = new C();
D cd4 = new D(c.M3);
cd3 += cd4;
cd3(); // call M1, M2, M1, then M3
cd3 -= cd1; // remove last M1
cd3(); // call M1, M2, then M3
cd3 -= cd4;
cd3(); // call M1 then M2
cd3 -= cd2;
cd3(); // call M1
cd3 -= cd2; // impossible removal is benign
cd3(); // call M1
cd3 -= cd1; // invocation list is empty
Console.WriteLine( cd3 == null );
// cd3(70); // System.NullReferenceException thrown
cd3 -= cd1; // impossible removal
Console.WriteLine( cd3 == null ); }
}
4.1 delegate的更多相关文章
- [.NET] C# 知识回顾 - 委托 delegate (续)
C# 知识回顾 - 委托 delegate (续) [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6046171.html 序 上篇<C# 知识回 ...
- [C#] C# 知识回顾 - 委托 delegate
C# 知识回顾 - 委托 delegate [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6031892.html 目录 What's 委托 委托的属性 ...
- iOS 键盘添加完成按钮,delegate和block回调
这个是一个比较初级一点的文章,新人可以看看.当然实现这个需求的时候自己也有一点收获,记下来吧. 前两天产品要求在工程的所有数字键盘弹出时,上面带一个小帽子,上面安装一个“完成”按钮,这个完成按钮也没有 ...
- C# 委托Delegate(一) 基础介绍&用法
本文是根据书本&网络 前人总结的. 1. 前言 定义&介绍: 委托Delegate是一个类,定义了方法的类型, 使得可以将方法当做另一个方法的参数来进行传递,这种将方法动态地赋给参数的 ...
- Jquery中的bind(),live(),delegate(),on()绑定事件方式
博客转载为作者:枫上善若水http://www.cnblogs.com/xilipu31/p/4105794.html 前言 因为项目中经常会有利用jquery操作dom元素的增删操作,所以会涉及到d ...
- C#基础知识六之委托(delegate、Action、Func、predicate)
1. 什么是委托 官方解释 委托是定义方法签名的类型,当实例化委托时,您可以将其实例化与任何具有兼容签名的方法想关联,可以通过委托实例调用方法. 个人理解 委托通俗一点说就是把一件事情交给别人来帮助完 ...
- [转载]C#委托和事件(Delegate、Event、EventHandler、EventArgs)
原文链接:http://blog.csdn.net/zwj7612356/article/details/8272520 14.1.委托 当要把方法作为实参传送给其他方法的形参时,形参需要使用委托.委 ...
- jQuery 中bind(),live(),delegate(),on() 区别(转)
当我们试图绑定一些事件到DOM元素上的时候,我相信上面这4个方法是最常用的.而它们之间到底有什么不同呢?在什么场合下用什么方法是最有效的呢? 准备知识: 当我们在开始的时候,有些知识是必须具备的: D ...
- UITableview delegate dataSource调用探究
UITableview是大家常用的UIKit组件之一,使用中我们最常遇到的就是对delegate和dataSource这两个委托的使用.我们大多数人可能知道当reloadData这个方法被调用时,de ...
- C#委托的介绍(delegate、Action、Func、predicate)
委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1.委托的声明 (1). delegate delegate我们常用到的一种声明 Deleg ...
随机推荐
- Windows下MongoDB安装
1.解压mongodb-win32-i386-1.8.1.zip ,创建路径d:\mongodb ,将解压后的Bin文件Copy to 此文件夹下 2.d:\mongodb 下建立Data文件夹 d: ...
- C++面向对象高级开发课程(第三周)
一,类与类之间的关系:继承(Inheritance).复合(Composition).委托(Delegation). 二,复合:表示 is-a ,该设计思想可以参照C语言的 struct . 1. 例 ...
- Nodejs学习笔记(四)与MySQL交互(felixge/node-mysql)
原文链接:http://www.cnblogs.com/zhongweiv/p/nodejs_mysql.html 介绍使用felixge/node-mysql进行SQL的增删改查以及断线重连等操作.
- Nodejs -- 使用koa2搭建数据爬虫
当前爬虫项目开发所需中间件: cheerio: 则能够对请求结果进行解析,解析方式和jquery的解析方式几乎完全相同 cheerio中文文档 开发参考node - cheerio模块 superag ...
- SQL Server @@参数一览表
--返回 SQL Server 自上次启动以来尝试的连接数,无论连接是成功还是失败. SELECT @@CONNECTIONS AS CONNECTIONS --返回 SQL Server 自上次启动 ...
- POJ 3468 A Simple Problem with Integers(线段树&区间更新)题解
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- 第五章 消息摘要算法--MAC
注意:本节内容主要参考自<Java加密与解密的艺术(第2版)>第6章“验证数据完整性--消息摘要算法” 5.1.mac(又称为Hmac) 原理:在md与sha系列算法的基础上加入了密钥,是 ...
- java中的抽象类和抽象方法
知识点:java中的抽象类和抽象方法 关键字abstract意为抽象的,可以用来修饰类和方法,分别称作抽象类和抽象方法 抽象类一般在多态的场景中使用 一:抽象类(abstract class) 在类的 ...
- 使用Android-studio开发移动app与weex结合开发详细步骤
详细步骤如下: 首先,确保机器已经安装了node.js,并且把npm更新到最新版本 下载完毕后,我们可以看到全局目录下的node_modules下面多出一个weex-toolkit 同时,我们留意 ...
- IDEA快捷键复习使用
https://www.jetbrains.com/help/idea/meet-intellij-idea.html 快捷键可以极快地进行代码编辑整理,在IDEA的快捷键中,除了有几个好像特别难按之 ...