ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Delegates 学习简记二

Delegates 学习简记二

原创 Linux操作系统 作者:kirinri 时间:2009-03-25 10:52:21 0 删除 编辑
Normal 0 false false false MicrosoftInternetExplorer4 Delegates 学习简记二

 

使用Delegate 变量调用方法

// Declare Delegate variable

GreetingDelegate deleEng, deleChi, deleAll;

deleEng = gp.EngGreeting;

deleChi = gp.ChiGreeting;

 

// delegate say english function

gp.GreetingPeople("delegate english", deleEng);

// delegate say chinese function

gp.GreetingPeople("delegate chinese", deleChi);

 

多方法绑定到同一委托

// 多方法绑定到同一委托

deleAll = gp.EngGreeting;

deleAll += gp.ChiGreeting;

gp.GreetingPeople("use the same delegate variable", deleAll);

 

直接调用委托方法

deleEng("delegate english");

deleChi("delegate chinese");

deleAll("use deleAll delegate variable");

 

移除委托中的方法

// remove ChiGreeting function

deleAll -= gp.ChiGreeting;

deleAll("use deleAll delegate variable, remove ChiGreeting function");

 

 

Program.cs

using System;

using System.Collections.Generic;

using System.Text;

 

namespace DelegatesTest

{

    public delegate void GreetingDelegate(string name);

    public enum language

    {

        Eng,Chi

    }

 

    class GreetPeople

    {

        // Only english function

        public void GreetingPeople(string name)

        {

            EngGreeting(name);

        }

        // Select function

        public void GreetingPeople(string name, language lan)

        {

            // check language

            switch (lan)

            {

                case language.Eng:

                    EngGreeting(name);

                    break;

                case language.Chi:

                    ChiGreeting(name);

                    break;

            }

        }

 

        // Delegate function

        public void GreetingPeople(string name, GreetingDelegate MakeGreeting)

        {

            MakeGreeting(name);

        }

 

 

 

        // say english function

        public void EngGreeting(string name)

        {

            Console.WriteLine("english---> Morning " + name);

        }

 

        // say chinese function

        public void ChiGreeting(string name)

        {

            Console.WriteLine("chinese---> 干啥呢?" + name);

        }

    }

}

 

GreetPeople.cs

using System;

using System.Collections.Generic;

using System.Text;

 

namespace DelegatesTest

{

    class Program

    {

        static void Main(string[] args)

        {

            GreetPeople gp = new GreetPeople();

 

            // Declare Delegate variable

            GreetingDelegate deleEng, deleChi, deleAll;

            deleEng = gp.EngGreeting;

            deleChi = gp.ChiGreeting;

 

            // 多方法绑定到同一委托

            deleAll = gp.EngGreeting;

            deleAll += gp.ChiGreeting;

 

 

            // say english function

            gp.GreetingPeople("nameTest");

 

            // select say english

            gp.GreetingPeople("english", language.Eng);

 

            // select say chinese

            gp.GreetingPeople("chinese", language.Chi);

 

            Console.WriteLine("-------------delegate-----function---------------------");

 

            // delegate say english function

            gp.GreetingPeople("delegate english", gp.EngGreeting);

            // delegate say chinese function

            gp.GreetingPeople("delegate chinese", gp.ChiGreeting);

 

            Console.WriteLine("------use delegate variable-------delegate-----function---------------------");

 

            // delegate say english function

            gp.GreetingPeople("delegate english", deleEng);

            // delegate say chinese function

            gp.GreetingPeople("delegate chinese", deleChi);

 

            Console.WriteLine("------use the same delegate variable-------delegate-----function---------------------");

 

            gp.GreetingPeople("use the same delegate variable", deleAll);

 

            // 直接调用委托方法

            deleEng("delegate english");

            deleChi("delegate chinese");

            deleAll("use deleAll delegate variable");

 

            // Remove ChiGreeting function

            deleAll -= gp.ChiGreeting;

            Console.WriteLine("------------------------remove ChiGreeting function-------------------------------");

            deleAll("use deleAll delegate variable, remove ChiGreeting function");

 

 

 

        }

    }

}

 

english---> Morning nameTest

english---> Morning english

chinese---> 干啥呢? chinese

-------------delegate-----function---------------------

english---> Morning delegate english

chinese---> 干啥呢? delegate chinese

------use delegate variable-------delegate-----function---------------------

english---> Morning delegate english

chinese---> 干啥呢? delegate chinese

------use the same delegate variable-------delegate-----function----------------

-----

english---> Morning use the same delegate variable

chinese---> 干啥呢? use the same delegate variable

english---> Morning delegate english

chinese---> 干啥呢? delegate chinese

english---> Morning use deleAll delegate variable

chinese---> 干啥呢? use deleAll delegate variable

------------------------remove ChiGreeting function-----------------------------

--

english---> Morning use deleAll delegate variable, remove ChiGreeting function

Press any key to continue . . .

 

记:

使用委托可以将多个方法绑定到同一个委托变量,可以依次调用所有绑定的方法。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12476590/viewspace-578342/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2007-12-21

  • 博文量
    257
  • 访问量
    500292