Net中事件的高级用法之三
1.事件的高级应用
使用事件可以解除代码耦合
2.事件高级应用实例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace MyEvent
{
public delegate void MiaoEventDelegate();
public class Cat
{
public event MiaoEventDelegate MiaoEventHandler;
public void Miao()
{
Console.WriteLine("猫,苗一声");
Mouse.Run();
Dog.Wang();
Neighbor.Awake();
Stealer.Hide();
Baby.Dry();
}
public void MiaoEvent(){
Console.WriteLine("**************MiaoEvent*****************");
Console.WriteLine("另外一只猫,喵一声");
if (MiaoEventHandler !=null)
{
MiaoEventHandler();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace MyEvent
{
public class Baby
{
public static void Dry()
{
Console.WriteLine("小孩子哭");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace MyEvent
{
public class Dog
{
public static void Wang()
{
Console.WriteLine("狗叫");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace MyEvent
{
public class Mouse
{
public static void Run()
{
Console.WriteLine("老鼠跑");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace MyEvent
{
public class Neighbor
{
public static void Awake()
{
Console.WriteLine("邻居被吵醒");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace MyEvent
{
public class Stealer
{
public static void Hide(){
Console.WriteLine("小偷藏起来");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace MyEvent
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("欢迎来的流星小子博客学习");
Cat cat = new Cat();
// cat.Miao();
cat.MiaoEventHandler += Mouse.Run;
cat.MiaoEventHandler += Dog.Wang;
cat.MiaoEventHandler += Neighbor.Awake;
cat.MiaoEventHandler += Stealer.Hide;
cat.MiaoEventHandler += Baby.Dry;
cat.MiaoEvent(); Console.Read();
}
}
}
Net中事件的高级用法之三的更多相关文章
- 详解Vue中watch的高级用法
我们通过实例代码给大家分享了Vue中watch的高级用法,对此知识点有需要的朋友可以跟着学习下. 假设有如下代码: <div> <p>FullName: {{fullName} ...
- linux中find命令高级用法
前言 在<Linux中的文件查找技巧>一文中,我们已经知道了文件查找的基本方法,今天我们介绍find命令的一些高级使用技巧.它能满足我们一些更加复杂的需求. 查找空文件或空目录 有时候需要 ...
- RAC中常见的高级用法-组合
组合: concat组合: 按一定顺序执行皇上与皇太子关系 concat底层实现: 1.当拼接信号被订阅,就会调用拼接信号的didSubscribe 2.didSu ...
- 【转】Vue.js中 watch 的高级用法
假设有如下代码: <div> <p>FullName: {{fullName}}</p> <p>FirstName: <input type=&q ...
- Vue.js中 watch 的高级用法
假设有如下代码: <div> <p>FullName: {{fullName}}</p> <p>FirstName: <input type=&q ...
- SQL语句中的select高级用法
#转载请联系 为了更好的了解下面的知识点,我们先创建两张表并插入数据. # 学生表 +----+-----------+------+--------+--------+--------+------ ...
- Flutter 中渐变的高级用法
Flutter 中渐变有三种: LinearGradient:线性渐变 RadialGradient:放射状渐变 SweepGradient:扇形渐变 看下原图,下面的渐变都是在此图基础上完成. Li ...
- ios中webview的高级用法(二)
框架: webview与js的通信框架 #import "MJViewController.h" #import "MBProgressHUD+Add.h" ...
- RAC中常见的高级用法-bind方法
RAC操作思想: Hook(钩子)思想 RAC核心方法:bind bind方法 假设想监听文本框的内容,并且在每次输出结果的时候,都在文本框的内容拼接一段文字" ...
随机推荐
- Idea快捷键---根据自己使用情况持续更新
查看接口的实现类 -->ctrl+alt+b 查看继承关系 -->ctrl+h 快速查看上次查看代码的位置: -->ctrl+alt+方向键(注意与intel显卡快捷键的冲突,如有冲 ...
- ReactiveCocoa 学习资料
之前就有听说,感觉很强大,ReactiveCocoa更加被Mattt Thompson大神称为开启一个新Objective-C纪元.所以觉得非常有学习的必要了. 一些很好的学习资料: Reactive ...
- mybatis 字段类型映射一览表
- Azure AD Domain Service(二)为域服务中的机器配置 Azure File Share 磁盘共享
一,引言 Azure File Share 是支持两种认证方式的! 1)Active Directory 2)Storage account key 记得上次分析的 "Azure File ...
- 1Python运行Appium测试的例子
电脑系统配置:Windows7的64位 1.Python sample(Python示例)下载 https://github.com/appium/sample-code/tree/master/sa ...
- termux vim 配置 自动补全 遇到的问题
// 自动不全需要安装 pkg install proot -y // 用proot可以为手机没有root的用户来模拟一个root的环境,这里主要是经典的 Linux 文件系统布局上的模拟. //执行 ...
- Solution -「ARC 082D」Sandglass
\(\mathcal{Description}\) Link. 一个沙漏内共 \(Xg\) 沙,令初始时上半部分为 A,下半部分为 B.沙漏在 \(r_1,r_2,\cdots,r_n\) 时 ...
- CentOS7搭建yum源仓库(阿里源)
文章目录 注意:环境要求 1.配置服务器端yum 1.1.安装yum源工具 1.2.配置nginx 1.2.1.配置nginx页面目录 1.3.替换yum源文件 1.4.建立yum源仓库 2.配置客户 ...
- app内嵌H5网页(webviewJavaScriptBridge)
摘要:使用的插件为webviewJavaScriptBridge,app端需要引入一下这个包,html页面只需一段JS代码 与IOS交互 <! 申明交互(此处代码固定) > functio ...
- Linux安装Python xlrd、xlwt、xlutils模块
一.安装setuptools: 可以先打开setuptools的python官网看看setuptools软件包如何安装: 1 > wget https://bitbucket.org/pypa/ ...