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 ...
随机推荐
- UVA302 John's trip(欧拉回路)
UVA302 John's trip 欧拉回路 attention: 如果有多组解,按字典序输出. 起点为每组数据所给的第一条边的编号较小的路口 每次输出完额外换一行 保证连通性 每次输入数据结束后, ...
- Mysql的序列
Mysql的序列 Mysql自带的序列:字段设置为int,属性里面选上“自动增长”即可: 在插入数据的时候可以不插入该字段的值,mysql会自动处理:
- 01: MySql简介
MySQL其他篇 目录: 参考网站 1.1 数据库介绍 1.2 视图 1.3 触发器 1.4 事物 1.1 数据库介绍返回顶部 1.什么是数据库? 1. 数据库(Database)是按照数据结构来组织 ...
- 2016年蓝桥杯B组C/C++省赛(预选赛)试题
2016年蓝桥杯B组C/C++ 点击查看2016年蓝桥杯B组省赛题目解析(答案) 第一题:煤球数目 有一堆煤球,堆成三角棱锥形.具体: 第一层放1个, 第二层3个(排列成三角形), 第三层6个(排列成 ...
- ACM-ICPC 2018 徐州赛区网络预赛H Ryuji doesn't want to study(树状数组)题解
题意:给你数组a,有两个操作 1 l r,计算l到r的答案:a[l]×L+a[l+1]×(L−1)+⋯+a[r−1]×2+a[r] (L is the length of [ l, r ] that ...
- OJ上 编译器 G++和C++的区别
原文 :http://blog.csdn.net/febr2/article/details/52068357 编译时的差异: 编译器优化不同: 举个栗子: ①: a++ ②: ++a 从标准C的角度 ...
- CodeForces 1029E div3
题目链接 第一道场上自己做出来的E题...虽然是div3,而且是原题... 当时做完ABC,D题没有思路就去怼E了,然后发现貌似原题? 事实上就是原题... 给个原题链接... [HNOI2003]消 ...
- 第四次程序设计作业 C++计算器计算及命令行的使用 前缀表达式方法实现
关键词:前缀中缀后缀表达式 波兰式 命令行 myGithub 一.前言 很有意思的开发和学习经历,从刚刚开始看到作业思考半天到现在的Debug过程,对我来说都或多或少有所提升. 也许这个时候自己挺迷茫 ...
- 04_kafka python客户端_Producer模拟
使用的python库: kafka-python 安装方式: pip install kafka-python 简单的模拟Producer """ Kafka Produ ...
- POJ 1681 Painter's Problem(高斯消元+枚举自由变元)
http://poj.org/problem?id=1681 题意:有一块只有黄白颜色的n*n的板子,每次刷一块格子时,上下左右都会改变颜色,求最少刷几次可以使得全部变成黄色. 思路: 这道题目也就是 ...