看陳廣老師c#參考視頻總結(第六篇)

日期:2008-6-11
學習內容:委託
遺留問題:今天的內容比較模糊,需要進一步理解
學習總結:
1.       委託
知識點一:委託聲明定義了一種類型,它用一組特定的參數以及返回類型來封裝方法。對於靜態方法,委託對象封裝要調用的方法。對於實例方法,委託對象同時封裝一個實例和該實例的一個方法。如果您有一個委託對象和一組適當的參數,則可以用這些參數調用該委託。
知識點二:委託本質是一個類(可以通過查看IL代碼證實),他是C++中函數指針的替代品;C++中函數的指針只能指向靜態的方法,而在C#中也能指向實例對象。
使用委託的代碼實例:
using System;

delegate void eatMydelegate(string food);

class Mydelegate

{

    static void zsEat(string food)

    {

        Console.WriteLine("張三吃"+food);

    }

    static void lsEat(string food)

    {

        Console.WriteLine("李四吃"+food);

    }

    static void wwEat(string food)

    {

        Console.WriteLine("王五吃"+food);

    }

    static void Main()

    {

        eatMydelegate zs=new eatMydelegate(zsEat);

        eatMydelegate ls=new eatMydelegate(lsEat);

        eatMydelegate ww=new eatMydelegate(wwEat);

        zs("西瓜");

        ls("西瓜");

        ww("西瓜");

       

    }

}

上述代碼中三個委託調用同一個參數(西瓜),過於繁瑣有沒有什麼解決辦法呢?委託鏈的引入

using System;

delegate void eatMydelegate(string food);

class Mydelegate

{

    static void zsEat(string food)

    {

        Console.WriteLine("張三吃"+food);

    }

    static void lsEat(string food)

    {

        Console.WriteLine("李四吃"+food);

    }

    static void wwEat(string food)

    {

        Console.WriteLine("王五吃"+food);

    }

    static void Main()

    {

        eatMydelegate zs=new eatMydelegate(zsEat);

        eatMydelegate ls=new eatMydelegate(lsEat);

        eatMydelegate ww=new eatMydelegate(wwEat);

        eatMydelegate eatChain;//定義委託連

        Console.WriteLine("張三,李四,王五,開座談會");

        eatChain = zs + ls + ww;

        eatChain("西瓜");//給委託鏈賦參數

        Console.WriteLine("李四出去接電話");

        eatChain -= ls;//c#重載了+=-=運算符,因此可以創建可變的委託鏈

        eatChain("香蕉");

        Console.WriteLine("李四回來了");

        eatChain += ls;

        eatChain("橘子");

    }

}

.net framework2.0引入的新特性,匿名方法,來簡化上面的代碼

using System;

delegate void eatMydelegate(string food);

class Mydelegate

{

    static void Main()

    {

        eatMydelegate eatChain;

        eatChain = null;

        eatChain += delegate(string food) { Console.WriteLine("張三吃" + food); };//.net framework2.0引入的新特性

        eatChain += delegate(string food) { Console.WriteLine("李四吃" + food); };

        eatChain += delegate(string food) { Console.WriteLine("王五吃" + food); };

        eatChain("香蕉");

    }

}

動態方法代理:
using System;

delegate void eatMydelegate(string food);

class Man

{

    private string name;

    public Man(string name)

    {

         this.name = name;

    }

    public void eat(string food)

    {

        Console.WriteLine(name + "" + food);

    }

}

class Party

{

    static void eatTogerther(string food, params eatMydelegate[] values)

    {

        if (values == null)

        {

            Console.WriteLine("座談會結束");

        }

        else

        {

            eatMydelegate eatChain = null;

            foreach (eatMydelegate ed in values)

                eatChain += ed;

            eatChain(food);

            Console.WriteLine();

        }

    }

    static void Main()

    {

        Man ZS = new Man("張三");

        Man LS = new Man("李四");

        Man WW = new Man("王五");

        eatMydelegate zs = new eatMydelegate(ZS.eat);

        eatMydelegate ls = new eatMydelegate(LS.eat);

        eatMydelegate ww = new eatMydelegate(WW.eat);

        Console.WriteLine("張三,李四,王五,開座談會");

        eatTogerther("西瓜",zs,ls,ww);

        Console.WriteLine("李四出去接電話");

        eatTogerther("香蕉",zs,ww);

        Console.WriteLine("李四回來了");

        eatTogerther("橘子",zs,ls,ww);

        eatTogerther(null,null);

    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章