C#委托和事件

贴一个博客地址:张子阳

http://www.tracefact.net/CSharp-Programming/Delegates-and-Events-in-CSharp.aspx

结论:

委托是函数指针。能调用和委托函数签名相同函数。

事件是它封装了委托类型的变量,在类的外部是private的。


程序:CCboy养成

CCboy->toSuperStar->membersDo

program类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 事件
{
    class Program
    {
        static void Main(string[] args)
        {
            CCboy exo = new CCboy();
            Console.WriteLine("老总,您好。您已经创造了偶像组合CCboy。");
            Console.WriteLine("让我们干点事吧");
            //exo.rankUpdate = delegate() { DoseList.PubllisTheSecondAlbum(10); };//=是报错,只能+=或-=
            exo.fate();
            Console.ReadKey();
        }
    }
}

各种功能类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 事件
{
    static class DoseList
    {
        public static void GetWards()
        {
            Console.WriteLine("获得各种奖项");
        }

        public static void SellMuch()
        {
            Console.WriteLine("CCBoys连2张专辑卖破百万张,创X国新历史!");
        }

        public static void New10(int year)
        {
            Console.WriteLine("{0}年,成为大势新人", year);
        }

        public static void PubllisTheFirstAlbum(int year)
        {
            Console.WriteLine("{0}年,出版第一张专辑<BOYs GO>",year);
        }

        public static void PubllisTheSecondAlbum(int year)
        {
            Console.WriteLine("{0}年,出版第一张专辑<go BOYS fight>", year);
        }

        public static void SubscribAdd()
        {
            Console.WriteLine("签下很多广告");
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 事件
{
    class membersDo
    {
        public delegate void selectWay();
        public selectWay byPeople;
        Talyer t = new Talyer();
        Keven k = new Keven();
        Lucas l = new Lucas();

        public void doseWhat()
        {
          
            byPeople += k.KevenDo;
            byPeople += l.lucasDo;
            byPeople += t.TaylerDo;

            if (byPeople!=null)
            {
                byPeople();
            }
        }

    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 事件
{
    class ToSuperStar
    {
        public delegate void reaction();
        public event reaction GetMuch;
        
       
        public void boomResult()
        {
            GetMuch += DoseList.SellMuch;
            GetMuch += DoseList.GetWards;
            GetMuch += DoseList.SubscribAdd;
        
            if (GetMuch!=null)
            {
                GetMuch();
            }

            Console.WriteLine("作为当红组合的成员该做什么呢?");
            membersDo member = new membersDo();
            member.doseWhat();
        }
	
		 
	
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 事件
{
    class CCboy
    {

        private int age1 = 2012, age2 = 2016;
       // private int[] history = { 2012, 2012, 2015 };
        public delegate void Recod();
        public event Recod rankUpdate;

        public void fate()
        {
                   
            rankUpdate +=delegate() {DoseList.New10(age1);};
            rankUpdate +=delegate() {DoseList.PubllisTheFirstAlbum(age1); };
            rankUpdate +=delegate() {DoseList.PubllisTheSecondAlbum(age2);};
            
            if (rankUpdate!=null)
            {
                //for (int i = 0; i < history.Length; i++)
                //{
                //    rankUpdate(history[i]);
                //}

                rankUpdate();

                Console.WriteLine("已经红了,做点别的吧");

                ToSuperStar ts = new ToSuperStar();
                ts.boomResult();

               
            }
        }
    }
}




组合成员类


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 事件
{
    class Keven
    {
        public void KevenDo()
        {
            Console.WriteLine("我是Kenven,我要解约");
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 事件
{
    class Lucas
    {
        public void lucasDo()
        {
            Console.WriteLine("我是lucas,我要解约");
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 事件
{
    class Talyer
    {
        public void TaylerDo()
        {
            Console.WriteLine("我是Tayler,我要解约");
        }
    }
}



posted on 2016-04-05 19:20  雪峰00  阅读(124)  评论(0)    收藏  举报

导航