static void Main(string[] args) { try { Console.WriteLine("欢迎来到.net高级班VIP课程,今天是Eleven老师为大家带来的委托事件的学习"); #region 委托 { //Console.WriteLine("****************************MyDelegate*************************"); MyDelegate myDelegate = new MyDelegate(); myDelegate.Show(); } { //Console.WriteLine("****************************委托解耦,代码复用*************************"); //Student student = new Student() //{ // Id = 123, // Name = "Rabbit", // Age = 23, // ClassId = 1 //}; //Student student1; ////上端还不是得知道是哪个国家的人? //student.Study(); //student.SayHi("大脸猫", PeopleType.Chinese); //student.SayHi("icefoxz", PeopleType.Malaysia); //student.SayHiChinese("大脸猫"); //{ // SayHiDelegate method = new SayHiDelegate(student.SayHiChinese); // student.SayHiPerfact("大脸猫", method); //} //{ // SayHiDelegate method = new SayHiDelegate(student.SayHiAmerican); // student.SayHiPerfact("PHS", method); //} //{ // SayHiDelegate method = new SayHiDelegate(student.SayHiMalaysia); // student.SayHiPerfact("icefoxz", method); //} } #endregion #region Action Func { Console.WriteLine("****************************ActionFunc*************************"); MyDelegate myDelegate = new MyDelegate(); myDelegate.Show(); } { Console.WriteLine("****************************Event*************************"); Cat cat = new Cat(); cat.Miao(); Console.WriteLine("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"); cat.CatMiaoAction += new Dog().Wang; cat.CatMiaoAction += new Mouse().Run; cat.CatMiaoAction += new Baby().Cry; cat.CatMiaoAction += new Mother().Wispher; cat.CatMiaoAction.Invoke(); cat.CatMiaoAction = null; cat.CatMiaoAction += new Brother().Turn; cat.CatMiaoAction += new Father().Roar; cat.CatMiaoAction += new Neighbor().Awake; cat.CatMiaoAction += new Stealer().Hide; cat.MiaoDelegate(); //去除依赖,Cat稳定了;还可以有多个Cat实例 Console.WriteLine("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"); cat.CatMiaoActionHandler += new Dog().Wang; cat.CatMiaoActionHandler += new Mouse().Run; cat.CatMiaoActionHandler += new Baby().Cry; //cat.CatMiaoActionHandler.Invoke(); //cat.CatMiaoActionHandler = null; cat.CatMiaoActionHandler += new Mother().Wispher; cat.CatMiaoActionHandler += new Brother().Turn; cat.CatMiaoActionHandler += new Father().Roar; cat.CatMiaoActionHandler += new Neighbor().Awake; cat.CatMiaoActionHandler += new Stealer().Hide; cat.MiaoEvent(); Console.WriteLine("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"); cat.AddObserver(new Dog()); cat.AddObserver(new Mouse()); cat.AddObserver(new Baby()); cat.AddObserver(new Mother()); cat.AddObserver(new Brother()); cat.AddObserver(new Father()); cat.AddObserver(new Neighbor()); cat.AddObserver(new Stealer()); cat.MiaoObserver(); } #endregion { EventStandard.Show(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.Read(); }
static void Main(string[] args) { try { //我发现昨天的关于判断哪国人大招呼素衣永委托代替枚举判断有问题啊,试两数据场景。比如枚举方式参数是传哪国人,自行判断,而委托我告诉他去判断哪国人久试哪国人 //答案:使用委托可以需要执行的逻辑提到上端调用方来定义了,直接把逻辑传递过去了 // 课程准时20:00开始 // 同时也希望同学们在每学习一个知识点后,能考虑把知识点应用到自己的作业中去! // 音效听着有问题,打个2 // 准备好学习 刷个1 //这里是直播课 //能听到老师讲话,能看到老师屏幕的 刷个1 // 事件在哪些场景中会使用? // Winform--WPF--请求级事件--Webform // 事件可以把固定动作写好,把可变动作分离出去,在满足条件的情况下执行可变动作,相当于对外开放了一个接口,由外部任意扩展 // 正是因为有这个特点,这就是正是封装框架所需要的,框架可以支持扩展 //控件事件: //启动Form---初始化控件Button---Click +=动作 // 点击按钮--鼠标操作--系统收集信号--判断发给程序---程序接受信号--判断控件--登录-(事件只能在所在类的内部触发)Button触发Click // 点击按钮--鼠标操作--系统收集信号--判断发给程序---程序接受信号--判断控件--支付-(事件只能在所在类的内部触发)Button触发Click //把公共部分的逻辑封装起来,把可变的逻辑通过事件发布出来; //这里两个按钮大部分的逻辑是一样的,可变的逻辑仅仅只是在注册到事件的方法里面 //Event:权限控制,防止外部乱来; Console.WriteLine("欢迎来到.net高级班VIP课程_委托_delegate + Event,我是Richard老师"); { //Console.WriteLine("****************************MyDelegate*************************"); MyDelegate myDelegate = new MyDelegate(); myDelegate.Show(); } { //Student student = new Student() //{ // Id = 123, // Name = "Richard", // Age = 23, // ClassId = 1 //}; //student.SayHi("飞哥", PeopleType.Chinese); //使用枚举可以让调用方避免出错 //SayHiDelegate sayHiDelegateChinese = new SayHiDelegate(student.SayHiChinese); //Action<string> act = new Action<string>(student.SayHiChinese); //student.SayHiShow("飞哥", new SayHiDelegate(act)); //SayHiDelegate sayHiDelegateAmerican = new SayHiDelegate(student.SayHiAmerican); //student.SayHiShow("飞哥", new SayHiDelegate(sayHiDelegateAmerican)); //SayHiDelegate sayHiDelegateJapanese = new SayHiDelegate(student.SayHiJapanese); //student.SayHiShow("飞哥", new SayHiDelegate(sayHiDelegateJapanese)); //现在是21:03 大家开始提问+上厕所,21:05 开始答疑,期间老师不说话 } { //Console.WriteLine("****************多播委托********************"); //Cat cat = new Cat(); //cat.Miao(); //Console.WriteLine("****************观察者模式********************"); //cat.AddObserver(new Dog()); //狗叫了 //cat.AddObserver(new Mouse());//老鼠跑了 //cat.AddObserver(new Baby()); // 小孩哭了 //cat.AddObserver(new Mother()); //cat.AddObserver(new Father()); //cat.AddObserver(new Neighbor()); //cat.AddObserver(new Stealer()); //cat.MiaoAbserver(); //Console.WriteLine("****************多播委托********************"); //cat.CatMaioAction += new Dog().Wang; //狗叫了 //cat.CatMaioAction += new Mouse().Run;//老鼠跑了 //cat.CatMaioAction += new Baby().Cry; // 小孩哭了 //cat.CatMaioAction += new Mother().Wispher; ////cat.CatMaioAction = null; ////cat.CatMaioAction.Invoke(); //cat.CatMaioAction += new Father().Roar; //cat.CatMaioAction += new Neighbor().Awake; //cat.CatMaioAction += new Stealer().Hide; //cat.MiaoDelegate(); //Console.WriteLine("****************事件Event********************"); //cat.CatMaioActionHandler += new Dog().Wang; //狗叫了 //cat.CatMaioActionHandler += new Mouse().Run;//老鼠跑了 //cat.CatMaioActionHandler += new Baby().Cry; // 小孩哭了 ////cat.CatMaioActionHandler = null; ////cat.CatMaioActionHandler.Invoke(); //cat.CatMaioActionHandler += new Mother().Wispher; //cat.CatMaioActionHandler += new Father().Roar; //cat.CatMaioActionHandler += new Neighbor().Awake; //cat.CatMaioActionHandler += new Stealer().Hide; //cat.MiaoEvent(); } { EventStandard.Show(); // 大家开始提问,22:10分开始答疑 } } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.Read(); }
static void Main(string[] args) { try { Console.WriteLine("********************Delegate + Event***********************"); { MyDelegate myDelegate = new MyDelegate(); myDelegate.Show(); } { ListExtend test = new ListExtend(); test.Show(); } { Console.WriteLine("*************************Event********************"); { Cat cat = new Cat(); cat.Miao(); } { Cat cat = new Cat(); cat.miaoDelegateHandler += new MiaoDelegate(new Mouse().Run); cat.miaoDelegateHandler += new MiaoDelegate(new Baby().Cry); cat.miaoDelegateHandler += new MiaoDelegate(new Mother().Wispher); cat.miaoDelegateHandler += new MiaoDelegate(new Father().Roar); cat.miaoDelegateHandler += new MiaoDelegate(new Neighbor().Awake); cat.miaoDelegateHandler += new MiaoDelegate(new Stealer().Hide); { cat.miaoDelegateHandler.Invoke(); cat.miaoDelegateHandler = null; } cat.MiaoNew(); } { Cat cat = new Cat(); cat.miaoDelegateEvent += new MiaoDelegate(new Mouse().Run); cat.miaoDelegateEvent += new MiaoDelegate(new Baby().Cry); cat.miaoDelegateEvent += new MiaoDelegate(new Mother().Wispher); cat.miaoDelegateEvent += new MiaoDelegate(new Father().Roar); cat.miaoDelegateEvent += new MiaoDelegate(new Neighbor().Awake); cat.miaoDelegateEvent += new MiaoDelegate(new Stealer().Hide); { // event 关键字不允许外部直接调用或者赋值 // 做了限制 //cat.miaoDelegateEvent.Invoke(); //cat.miaoDelegateEvent = null; } cat.MiaoNew(); } } } catch (Exception e) { Console.WriteLine(e.Message); } Console.Read(); }