日期: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);
    }
}