4.1 delegate

时间:2022-09-30 00:52:49

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 ); }
}