委托示例

  1. using System;
  2.  
  3. delegate double Fun( double x );
  4.  
  5.  
  6.  
  7. public class DelegateIntegral
  8. {
  9.     public static void Main()
  10.     {
  11.         Fun fun = new Fun(Math.Sin);
  12.         double d = Integral( fun, 0, Math.PI/2, 1e-4 ); 
  13.         Console.WriteLine( d );
  14.  
  15.         Fun fun2 = new Fun( Linear );
  16.         double d2 = Integral( fun2, 0, 2, 1e-3 );
  17.         Console.WriteLine( d2 );
  18.  
  19.         Rnd rnd = new Rnd();
  20.         double d3 = Integral( new Fun(rnd.Num), 0, 1, 0.01 ); 
  21.         Console.WriteLine( d3 );
  22.     }
  23.      
  24.     static double Linear( double a )
  25.     {
  26.         return a*2+1;
  27.     }
  28.      
  29.     class Rnd 
  30.     {
  31.         Random r = new Random();
  32.         public double Num( double  x )
  33.         {
  34.             return r.NextDouble();
  35.         }
  36.     }
  37.  
  38.     static double Integral(Fun f, double a, double b, double eps)// 积分计算
  39.     {
  40.         int n,k;
  41.         double fa,fb,h,t1,p,s,x,t=0;
  42.  
  43.         fa=f(a); 
  44.         fb=f(b);
  45.  
  46.         // 迭代初值
  47.         n=1; 
  48.         h=b-a;
  49.         t1=h*(fa+fb)/2.0;
  50.         p=double.MaxValue;
  51.  
  52.         // 迭代计算
  53.         while (p>=eps)
  54.         
  55.             s=0.0;
  56.             for (k=0;k<=n-1;k++)
  57.             
  58.                 x=a+(k+0.5)*h;
  59.                 s=s+f(x);
  60.             }
  61.  
  62.             t=(t1+h*s)/2.0;
  63.             p=Math.Abs(t1-t);
  64.             t1=t; 
  65.             n=n+n; 
  66.             h=h/2.0;
  67.         }
  68.         return t;
  69.     }
  70.  
  71. }




  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7.  
  8. namespace TestWin
  9. {
  10.     /// <summary>
  11.     /// Summary description for Form1.
  12.     /// </summary>
  13.     public class Form1 : System.Windows.Forms.Form
  14.     {
  15.         private System.Windows.Forms.Button button1;
  16.         /// <summary>
  17.         /// Required designer variable.
  18.         /// </summary>
  19.         private System.ComponentModel.Container components = null;
  20.  
  21.         public Form1()
  22.         {
  23.             //
  24.             // Required for Windows Form Designer support
  25.             //
  26.             InitializeComponent();
  27.  
  28.             //
  29.             // TODO: Add any constructor code after InitializeComponent call
  30.             //
  31.         }
  32.  
  33.         /// <summary>
  34.         /// Clean up any resources being used.
  35.         /// </summary>
  36.         protected override void Dispose( bool disposing )
  37.         {
  38.             if( disposing )
  39.             {
  40.                 if (components != null
  41.                 {
  42.                     components.Dispose();
  43.                 }
  44.             }
  45.             base.Dispose( disposing );
  46.         }
  47.  
  48.         #region Windows Form Designer generated code
  49.         /// <summary>
  50.         /// Required method for Designer support - do not modify
  51.         /// the contents of this method with the code editor.
  52.         /// </summary>
  53.         private void InitializeComponent()
  54.         {
  55.             this.button1 = new System.Windows.Forms.Button();
  56.             this.SuspendLayout();
  57.             // 
  58.             // button1
  59.             // 
  60.             this.button1.Location = new System.Drawing.Point(216, 24);
  61.             this.button1.Name = "button1";
  62.             this.button1.Size = new System.Drawing.Size(64, 24);
  63.             this.button1.TabIndex = 0;
  64.             this.button1.Text = "button1";
  65.             // 
  66.             // Form1
  67.             // 
  68.             this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  69.             this.ClientSize = new System.Drawing.Size(292, 273);
  70.             this.Controls.AddRange(new System.Windows.Forms.Control[] {
  71.                                                                           this.button1});
  72.             this.Name = "Form1";
  73.             this.Text = "Form1";
  74.             this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
  75.             this.ResumeLayout(false);
  76.  
  77.         }
  78.         #endregion
  79.  
  80.         /// <summary>
  81.         /// The main entry point for the application.
  82.         /// </summary>
  83.         [STAThread]
  84.         static void Main() 
  85.         {
  86.             Application.Run(new Form1());
  87.         }
  88.  
  89.         private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  90.         {
  91.             Graphics g = e.Graphics;
  92.             Pen pen = Pens.Blue;
  93.  
  94.             Fun [] funs = {
  95.                               new Fun( this.Square ),
  96.                               new Fun( Form1.XPlus ),
  97.                               new Fun( Math.Cos ),
  98.                               new Fun( Math.Sqrt )
  99.                           };
  100.             foreach( Fun fun in funs )
  101.             {
  102.                 PlotFun( fun, g, pen );
  103.             }
  104.         }
  105.  
  106.         delegate double Fun( double x );
  107.  
  108.         void PlotFun( Fun fun, Graphics g, Pen pen )
  109.         {
  110.                 fordouble x=0; x<10; x+=0.1)
  111.                 {
  112.                     double y = fun(x);
  113.                     Point point = new Point( (int)(x*20), (int)(200-y*30) );
  114.                     g.DrawLine( pen, point, new Point( point.X+2, point.Y+2) );
  115.                     Console.WriteLine( " " + x + " " + y );
  116.                 }
  117.         }
  118.  
  119.         double Square( double x )
  120.         {
  121.             return Math.Sqrt( Math.Sqrt( x) );
  122.         }
  123.         static double XPlus( double x )
  124.         {
  125.             return Math.Sin(x)+ Math.Cos(x*5)/5;
  126.         }
  127.  
  128.     }
  129. }




  1. using System;
  2. delegate void D(int x);
  3. class C
  4. {
  5.     public static void M1(int i) 
  6.     {
  7.         Console.WriteLine("C.M1: " + i);
  8.     }
  9.     public static void M2(int i) 
  10.     {
  11.         Console.WriteLine("C.M2: " + i);
  12.     }
  13.     public void M3(int i) 
  14.     {
  15.         Console.WriteLine("C.M3: " + i);
  16.     }
  17. }
  18. class Test
  19. {
  20.     static void Main() 
  21.     
  22.         D cd1 = new D( C.M1);
  23.         cd1(-1);        // call M1
  24.         D cd2 = null;
  25.         cd2 += new D( C.M2);
  26.         cd2(-2);        // call M2
  27.         D cd3 = cd1 + cd2;
  28.         cd3(10);        // call M1 then M2
  29.         cd3 += cd1;
  30.         cd3(20);        // call M1, M2, then M1
  31.         C c = new C();
  32.         D cd4 = new D(c.M3);
  33.         cd3 += cd4;
  34.         cd3(30);        // call M1, M2, M1, then M3
  35.         cd3 -= cd1;       // remove last M1
  36.         cd3(40);        // call M1, M2, then M3
  37.         cd3 -= cd4;
  38.         cd3(50);        // call M1 then M2
  39.         cd3 -= cd2;
  40.         cd3(60);        // call M1
  41.         cd3 -= cd2;       // impossible removal is benign
  42.         cd3(60);        // call M1
  43.         cd3 -= cd1;       // invocation list is empty
  44.         Console.WriteLine( cd3 == null );
  45.         //      cd3(70);    // System.NullReferenceException thrown
  46.         cd3 -= cd1;       // impossible removal
  47.         Console.WriteLine( cd3 == null );
  48.  
  49.     }
  50. }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值