• 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, 0, Math.PI/2, 1e-4 ); 
            Console.WriteLine( d );
     
            Fun fun2 = new Fun( Linear );
            double d2 = Integral( fun2, 0, 2, 1e-3 );
            Console.WriteLine( d2 );
     
            Rnd rnd = new Rnd();
            double d3 = Integral( new Fun(rnd.Num), 0, 1, 0.01 ); 
            Console.WriteLine( d3 );
        }
         
        static double Linear( double a )
        {
            return a*2+1;
        }
         
        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=0;
     
            fa=f(a); 
            fb=f(b);
     
            // 迭代初值
            n=1; 
            h=b-a;
            t1=h*(fa+fb)/2.0;
            p=double.MaxValue;
     
            // 迭代计算
            while (p>=eps)
            { 
                s=0.0;
                for (k=0;k<=n-1;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(216, 24);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(64, 24);
                this.button1.TabIndex = 0;
                this.button1.Text = "button1";
                // 
                // Form1
                // 
                this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
                this.ClientSize = new System.Drawing.Size(292, 273);
                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=0; x<10; x+=0.1)
                    {
                        double y = fun(x);
                        Point point = new Point( (int)(x*20), (int)(200-y*30) );
                        g.DrawLine( pen, point, new Point( point.X+2, point.Y+2) );
                        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*5)/5;
            }
     
        }
    }
    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(-1);        // call M1
            D cd2 = null;
            cd2 += new D( C.M2);
            cd2(-2);        // call M2
            D cd3 = cd1 + cd2;
            cd3(10);        // call M1 then M2
            cd3 += cd1;
            cd3(20);        // call M1, M2, then M1
            C c = new C();
            D cd4 = new D(c.M3);
            cd3 += cd4;
            cd3(30);        // call M1, M2, M1, then M3
            cd3 -= cd1;       // remove last M1
            cd3(40);        // call M1, M2, then M3
            cd3 -= cd4;
            cd3(50);        // call M1 then M2
            cd3 -= cd2;
            cd3(60);        // call M1
            cd3 -= cd2;       // impossible removal is benign
            cd3(60);        // 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 );
     
        }
    }
  • 相关阅读:
    nginx 启用php解析及解决无法访问报错Primary script unknown的过程
    百度云服务器连接码云的坑
    async函数测试
    centos6.8安装mysql5.7及用navicat远程连接
    JavaScript模块知识理解
    为什么if else 语句里不能用函数声明定义函数,而可以用函数表达式定义函数
    Java8-对map过滤
    git cherry-pick 的使用
    Java8-对map排序
    Java8过滤器(Filter)
  • 原文地址:https://www.cnblogs.com/CandiceW/p/4234046.html
Copyright © 2020-2023  润新知