zoukankan      html  css  js  c++  java
  • C# 委托的”四步走“

    看了一本《深入了解C#》感觉很不错,对于委托的讲解,给大家摘录了下来!

    1.什么是委托

      我的拙见:委托就是将方法作为参数,进行传递的

      书中的记载:将某种行为“包含”在一个对象中,这个对象可以像其他任何对象那样使用,在该对象中可以齿形对象的行为。可以选择将委托类型看做只定义一个方法的接口,将委托示例看做实现了那个接口的一个对象(感觉这些说发有点空洞)。

    2.简单委托的构成

     为了让委托做某事,必须满足4个条件

     (1)声明委托类型(2)必须有一个方法包含要执行的的代码(3)必须创建一个委托实例(4)必须调用(invoke)委托实例

    下面通过一个例子解释

     using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace weituo
    {
        class Program
        {
            static void Main(string[] args)
            {
                //(2)第二步:必须创建一个委托实例
                ProcessDelegate process;
                Console.WriteLine("请输入用逗号分隔的两个数字:");
                string input = Console.ReadLine();
                int commaPos = input.IndexOf(',');
                double param1 = Convert.ToDouble(input.Substring(0, commaPos));
                double param2 = Convert.ToDouble(input.Substring(commaPos + 1,input.Length - commaPos -1));
     
                Console.WriteLine("输入M乘法D除法");
                input =Console.ReadLine();
     
                // 初始化委托变量
                if(input =="M")
                       //(4)必须调用(invoke)委托实例
                    process = new ProcessDelegate(Multiply);
                    //注释:此处也可以写process = Multiply
                else
                         //(4)必须调用(invoke)委托实例
                    process = new ProcessDelegate(Divide);
     
                // 使用委托调用函数
                double result = process(param1,param2);
                Console.WriteLine("结果:{0}",result);
                Console.ReadKey();
     
            }
     
            //(1)第一步:声明委托类型
            delegate double ProcessDelegate(double param1,double param2);
           //(2)第二步:必须有一个方法包含要执行的的代码(下面是两个方法)
            static double Multiply(double param1, double param2)
            {
                return param1 * param2;
            }
     
            static double Divide(double param1, double param2)
            {
                return param1 / param2;
            }
     
        }
    }
    

      实例二:

    下面一个例子,自己表一下步骤,如果能找清楚,就应该大致明白了

      

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DemoConsole {
        //声明一个委托
        delegate string ConvertMethod(string inString);
        delegate void  SayHello(string sayString);
        public class Program {
            static void Main(string[] args) {
                #region MyRegion
                //string[] names = { "Hartono, Tommy", "Adams, Terry", "Andersen, Henriette Thaulow", "Hedlund, Magnus", "Ito, Shu" };
                //Random random = new Random(DateTime.Now.Millisecond);
                //string name = names.ElementAt(random.Next(0, names.Length));
                ////Console.WriteLine(name);
                ////Console.ReadKey();
                //Dictionary<int, string> dic = new Dictionary<int, string>();
                //dic.Add(1, "AAA");
                //dic.Add(2, "BBBB");
                //dic.Add(3, "CCCC");
                //bool t = dic.All(x => x.Key > 0);
                //bool df = dic.Any(x => x.Key > 0);
                //dic.Any(); 
                #endregion
                ConvertMethod convertMeth;
                string name = "Dakota";
                convertMeth = UppercaseString;
                Console.WriteLine(convertMeth(name));
                Person p1 = new Person();
                Person p2 = new Person();
                SayHello sp1, sp2;
                sp1 = p1.Say;
                sp2 = p2.Say;
                sp1("AAAA");
                sp2.Invoke("BBBBBB");
                Console.ReadKey();
    
            }
            private static string UppercaseString(string inputString) {
                return inputString.ToUpper();
            }
           
        }
        public class Person {
            public string Name { get; set; }
            public int Age { get; set; }
            public string Address { get; set; }
            public void Say(string say) {
                Console.WriteLine(this.Name+"__"+say);
            }
        }
    }
    

      

  • 相关阅读:
    shell脚本,文件里面的英文大小写替换方法。
    shell脚本,100以内的质数有哪些?
    shell脚本,当用sed删除某一文件里面的内容时,并追加到同一个文件会出现问题。
    shell脚本,按行读取文件的几种方法。
    shell脚本,锁机制
    shell脚本,通过一个shell程序计算n的阶乘。
    shell脚本,如何写进度条。
    shell脚本,判断给出的字符串是否相等。
    shell脚本,按空格开始60秒的倒计时。
    18:django 日志系统
  • 原文地址:https://www.cnblogs.com/alphafly/p/3985965.html
Copyright © 2011-2022 走看看