zoukankan      html  css  js  c++  java
  • 委托和匿名方法学习心得

                                              委托和匿名方法学习心得
    1:委托
           委托关键的一点是返回类型和参数列表必须要和所代表的方法的返回类型和参数一致
    下面是一个小例子(为使例子代码相对短点,简单点,我全部使用的是静态方法)
        例1:using System;
             using System.Collections.Generic;
             using System.Text;
             namespace ConsoleApplication1
               {
                    class Program
                     {
                         public delegate void GetCode(); //委托定义,返回类型和参数列表要相同哦
                         static void Main(string[] args)
                        {
                             //委托的构造函数的参数是一个方法,这点很重要
                         GetCode getcode = new GetCode(_MyClass.Shu);
                         getcode();
                         Console.ReadLine();
                        }
                    }

                  public class _MyClass
                   {
                     public static void Shu()
                      {
                         Console.WriteLine("用委托调用此方法");
                      }
                  }
            }
    备注:这个例子很简单,但有2点注意。1:委托必须用delegate定义。2:返回类型,参数列表必须要和所委托的方法一致。

      例2:带参数的委托

    using System;
    using System.Collections.Generic;
    using System.Text;
    namespace ConsoleApplication1
    {
        class Program
        {
            public delegate String  GetCode(string name);
            static void Main(string[] args)
            {
                String name = "xuting";
                GetCode getcode = new GetCode(_MyClass.Shu);
                String Chu = getcode(name);
                Console.WriteLine(Chu);
                Console.ReadLine();
            }
        }

        public class _MyClass
        {
            public static String Shu(string name)
            {
                string _name = name + "你好";
                return _name;
            }
        }
    }
    例2和例1没多大区别,我想大家因该能看的懂,下面是一个稍微复杂一点点的委托。调用的是委托列表。在委托例子全部完毕后,
    我将为大家解释一下匿名方法。匿名方法相比较而言要方便点,而且匿名方法在集合类用做比较,搜索等功能很方便。

    例3:复杂一点的委托

    using System;
    using System.Collections.Generic;
    using System.Text;
    namespace ConsoleApplication1
    {
        class Program
        {
            public delegate int GetCode(int x);
            static void Main(string[] args)
            {
                GetCode[] getcode = new GetCode[2];
                getcode[0] = new GetCode(_MyClass.Cheng);
                getcode[1] = new GetCode(_MyClass.Chu);
                /*
                int wo = getcode[0](4);
                Console.WriteLine(wo);
                 * 这两句用来测试委托用的
                 */
                for (int i = 0; i < getcode.Length; i++)
                {
                    AddShu(getcode[i], 4);
                    AddShu(getcode[i], 5);
                    AddShu(getcode[i], 6);
                }
                    Console.ReadLine();
            }
            public static void AddShu(GetCode x, int y)
            {
                int wo = x(y);//这句的意思和上面测试委托的意思一样哦
                Console.WriteLine("GetCode is {0},Y is{1}", wo, y);
            }
        }

        public class _MyClass
        {
            public static int Cheng(int x)
            {
                return x * 2;
            }
            public static int Chu(int x)
            {
                return x * x;
            }
        }
    }
    这个例子虽然复杂了一点,但我们可以这样理解,可以测试一下,不用数组,我们单独写个委托
    GetCode getcode=new GetCode(_MyClass.Cheng);
    int wo=getcode(4);
    Console.WriteLine(wo);而上面只不过用一个静态方法AddShu包含了这个意思,然后在主方法Mine里面用循环显示出来而已,意思一样,呵呵


    匿名方法
    往上看我们例1,其中我们为了使用委托,而定义了一个类Class,其中包括我们所要调用的静态方法。也就是说,要使用委托,
    在使用委托的构造函数时,必须已经定义了一个方法来供委托调用,所以使用委托必须要有现成的方法,而我们目前为止所学的知识
    好象也是这个意思,其实不然,看例4,下面把例1的例子稍微改动一下,就可以变为匿名方法了
    例4:匿名方法

    using System;
    using System.Collections.Generic;
    using System.Text;
    namespace ConsoleApplication1
    {
        class Program
        {
            public delegate void GetCode();
            static void Main(string[] args)
            {
                GetCode getcode = delegate()
                {
                    Console.WriteLine("匿名方法");
                };
                getcode();
                Console.ReadLine();
            }
        }

    },细心的朋友可能已经发现本例和例1的区别为:本例已经删除了自定义类_MyClass,用delegate()加一段();代码块取得了和自定义里面静态方法
    相同的效果。是的呀,匿名方法定义就是这样简单。现在我们大家肯定对匿名方法有一定的了解了吧,那我在把例3用匿名方法的形式写出来。哇,可以看见的,用匿名方法真的省去了不少的代码,而且很简单名了。

    例5:
        using System;
    using System.Collections.Generic;
    using System.Text;
    namespace ConsoleApplication1
    {
        class Program
        {
            public delegate int GetCode(int x);
            static void Main(string[] args)
            {
                GetCode[] getcode = new GetCode[2];
                getcode[0] =delegate(int x)
                {
                    return x*2;
                };
                getcode[1] = delegate(int x)
                {
                    return x * x;
                };
               
            for(int i = 0; i < getcode.Length; i++)
                {
                    AddShu(getcode[i], 4);
                    AddShu(getcode[i], 5);
                    AddShu(getcode[i], 6);
                }
                Console.ReadLine();
            }
            public static void AddShu(GetCode x, int y)
            {
                int wo = x(y);
                Console.WriteLine("GetCode is {0},Y is{1}", wo, y);
            }
        }

    }可能这个例子初学者不是特别容易理解,我现在把它改为更简单直观一点。
    using System;
    using System.Collections.Generic;
    using System.Text;
    namespace ConsoleApplication1
    {
        class Program
        {
            public delegate int GetCode(int x);
            static void Main(string[] args)
            {
                GetCode getcode = delegate(int x)
                {
                    return x * x;
                };
                AddShu(getcode, 6);
                Console.ReadLine();
            }
            public static void AddShu(GetCode x, int y)
            {
                int wo = x(y);
                Console.WriteLine("x is {0},y is {1}", wo, y);
            }
        }

    }。这样容易多了吧。嘻嘻。这个例子让我想起了当时学集合排序,查找的时候的一个例子,也是用的匿名方法,现在我也把代码贴出来吧
    foreach (_Myclass Mm in list.FindAll(delegate(_Myclass R) { return R.Age == 26; }))
                {
                    Console.WriteLine(Mm);
                }这是是代码的一个小片段,意思是用foreach拭代集合类的FindAll功能查找满足条件的记录,如果没有匿名方法,那我们还需要在编写一个功能类来用委托调用此方法。
    委托和匿名方法就写到这,其实还有多播委托等等,那相对比较难理解一点,希望网友能通过这遍文章学习为以后学习委托和匿名方法打好基础。

         

     

  • 相关阅读:
    一本书
    在linux oracle 10g/11g x64bit环境中,goldengate随os启动而自己主动启动的脚本
    关于仿酷狗音乐播放器开源:寻求一套音乐播放器素材,让仿酷狗开源
    Cocos2d-x中Vector&lt;T&gt;容器以及实例介绍
    成都传智播客Java/PHP培训就业率高
    P1341 无序字母对
    P1168 中位数
    P1146 硬币翻转
    P1340 兽径管理
    P2023 [AHOI2009]维护序列
  • 原文地址:https://www.cnblogs.com/xuting/p/1276318.html
Copyright © 2011-2022 走看看