C#委托和事件
2016-04-05 19:20
399 查看
贴一个博客地址:张子阳
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 Keven
{
public void KevenDo()
{
Console.WriteLine("我是Kenven,我要解约");
}
}
}
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 Sy 4000 stem; 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,我要解约"); } } }
相关文章推荐
- C# 基础知识 1
- C#多态之虚方法
- C#程序中session值的保存方法以及转为字符串的方法总结
- C#程序中session的基本设置示例及清除session的方法
- C#中DllImport用法
- C#实现属于自己的QQ截图工具
- C#一般处理程序 ashx.cs使用Session报错问题
- C#线程处理系列之线程池中的I/O线程
- C#委托基础:谈委托和接口
- C# 命名规范
- C#的一些零散东西
- C#基础知识---飞行棋小游戏
- C#List泛型集合
- C#StreamReader和StreamWriter
- C# Dictionary
- C# List小练习
- mono嵌入式应用研究(四):Assemly的加载与执行
- C#统计字符串中字符出现的次数
- C#键值对集合
- C# File类