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的更多相关文章

  1. [.NET] C# 知识回顾 - 委托 delegate (续)

    C# 知识回顾 - 委托 delegate (续) [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6046171.html 序 上篇<C# 知识回 ...

  2. [C#] C# 知识回顾 - 委托 delegate

    C# 知识回顾 - 委托 delegate [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6031892.html 目录 What's 委托 委托的属性 ...

  3. iOS 键盘添加完成按钮,delegate和block回调

    这个是一个比较初级一点的文章,新人可以看看.当然实现这个需求的时候自己也有一点收获,记下来吧. 前两天产品要求在工程的所有数字键盘弹出时,上面带一个小帽子,上面安装一个“完成”按钮,这个完成按钮也没有 ...

  4. C# 委托Delegate(一) 基础介绍&用法

    本文是根据书本&网络 前人总结的. 1. 前言 定义&介绍: 委托Delegate是一个类,定义了方法的类型, 使得可以将方法当做另一个方法的参数来进行传递,这种将方法动态地赋给参数的 ...

  5. Jquery中的bind(),live(),delegate(),on()绑定事件方式

    博客转载为作者:枫上善若水http://www.cnblogs.com/xilipu31/p/4105794.html 前言 因为项目中经常会有利用jquery操作dom元素的增删操作,所以会涉及到d ...

  6. C#基础知识六之委托(delegate、Action、Func、predicate)

    1. 什么是委托 官方解释 委托是定义方法签名的类型,当实例化委托时,您可以将其实例化与任何具有兼容签名的方法想关联,可以通过委托实例调用方法. 个人理解 委托通俗一点说就是把一件事情交给别人来帮助完 ...

  7. [转载]C#委托和事件(Delegate、Event、EventHandler、EventArgs)

    原文链接:http://blog.csdn.net/zwj7612356/article/details/8272520 14.1.委托 当要把方法作为实参传送给其他方法的形参时,形参需要使用委托.委 ...

  8. jQuery 中bind(),live(),delegate(),on() 区别(转)

    当我们试图绑定一些事件到DOM元素上的时候,我相信上面这4个方法是最常用的.而它们之间到底有什么不同呢?在什么场合下用什么方法是最有效的呢? 准备知识: 当我们在开始的时候,有些知识是必须具备的: D ...

  9. UITableview delegate dataSource调用探究

    UITableview是大家常用的UIKit组件之一,使用中我们最常遇到的就是对delegate和dataSource这两个委托的使用.我们大多数人可能知道当reloadData这个方法被调用时,de ...

  10. C#委托的介绍(delegate、Action、Func、predicate)

    委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1.委托的声明 (1). delegate delegate我们常用到的一种声明   Deleg ...

随机推荐

  1. cisco路由器 三层交换机简单环境配置实例(图)

    出处:http://www.jb51.NET/softjc/56600.html cisco路由器&三层交换机简单环境配置实例 一.网络拓扑图: 二.配置命令: 1.路由器的配置: inter ...

  2. adb shell 命令详解,android, adb logcat

    http://www.miui.com/article-275-1.html http://noobjava.iteye.com/blog/1914348 adb shell 命令详解,android ...

  3. VC++ 进度条更新方案

    在实际开发中,如果有耗时操作,一般会在工作线程处理数据,然后处理完成后把时间传递到UI线程进行显示,切记不要在工作线程对UI进行操作. 场景: 1. 很多程序需要根据处理业务的进度来更新进度条,进度条 ...

  4. Tomcat下使用Log4j,按日期每天存放,解决catalina.out日志文件过大问题

    1. 准备jar包: log4j-1.2.17.jar (从 http://www.apache.org/dist/logging/log4j/1.2.17/ 下载) tomcat-juli.jar, ...

  5. Code First技术介绍

    地址:https://wenku.baidu.com/view/5620b862eefdc8d376ee3258.html 仅供参考

  6. cogs 362. [CEOI2004]锯木厂选址

    ★★★   输入文件:two.in   输出文件:two.out   简单对比 时间限制:0.1 s   内存限制:32 MB 从山顶上到山底下沿着一条直线种植了n棵老树.当地的政府决定把他们砍下来. ...

  7. 第七章 对称加密算法--DES

    注意:本节内容主要参考自<Java加密与解密的艺术(第2版)>第7章“初等加密算法--对称加密算法” 7.1.对称加密算法 特点: 加密与解密使用同一个密钥 是使用最广的算法 常见对称加密 ...

  8. [不屈的复习] - 安装Java初始化环境

    点WIN键->运行(或者使用win+r) 输入cmd命令输入java -version 注: -version是小写,不能使用大写,java后面有一个空格 配置成功后,会出现版本信息 java ...

  9. hbase读写流程分析

    前言 最近被大佬问到一个问题,hbase查询数据在最坏的场景下需要进行几次rpc,当时就懵了..下面主要对client端代码进行分析.阅读文章和看源码更配~ 读数据 流程总览 1. 从zookeepe ...

  10. Python 获取文件的创建时间,修改时间和访问时间

    # 用到的知识# os.path.getatime(file) 输出文件访问时间# os.path.getctime(file) 输出文件的创建时间# os.path.getmtime(file) 输 ...