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方法 假设想监听文本框的内容,并且在每次输出结果的时候,都在文本框的内容拼接一段文字" ...
随机推荐
- 简单实现支付密码输入框 By HL
密码输入框在微信,支付宝中比较常见 主要功能点 1.6位(或者N位)密码输入框封装
- 按照递推的思想求解next[]数组
按照递推的思想求解next[]数组 根据定义next[0]=-1,假设next[j]=k, 即P[0...k-1]==P[j-k,j-1] 若P[j]P[k],则有P[0..k]P[j-k,j],很显 ...
- rabbitmq集群实现
官方文档 一.环境准备 1.1 IP地址规划 1.2 配置主机域名解析 ##每个节点修改主机名 # hostnamectl set-hostname mq1.example.local # hostn ...
- 论文翻译:2022_PACDNN: A phase-aware composite deep neural network for speech enhancement
论文地址:PACDNN:一种用于语音增强的相位感知复合深度神经网络 引用格式:Hasannezhad M,Yu H,Zhu W P,et al. PACDNN: A phase-aware compo ...
- tep集成mitmproxy录制流量自动生成用例
使用 操作过程非常简单,流程如下: ①配置过滤域名 必须配置,不然会有很多无效流量造成数据冗余. ②启动代理 「示例」使用了反向代理,tep自带FastApi启动Mock服务: 「实际」使用正向代理, ...
- Error from server error dialing backend remote error tls internal error
# kubectl exec -it mysql-master-8cfb64ff9-ct4dx -n prophet -- /bin/bash Error from server: error dia ...
- 面试突击24:为什么wait和notify必须放在synchronized中?
在多线程编程中,wait 方法是让当前线程进入休眠状态,直到另一个线程调用了 notify 或 notifyAll 方法之后,才能继续恢复执行.而在 Java 中,wait 和 notify/noti ...
- shell批量监控网站状态码
shell批量监控网站状态码脚本,使用curl很慢.等我学完其他方式,在来更新. #!/bin/bash #GuoYabin yuming=`/bin/cat yuming.txt` for i in ...
- 使用Java的GUI技术实现 “ 贪吃蛇 ” 游戏
详细教程: 使用Java的GUI技术实现 " 贪吃蛇 " 游戏_IT打工酱的博客-CSDN博客
- OpenGL ES 3D空间中自定义显示空间
在Android中,我们所熟知的是在ES管线中,其在图元装配时,会进行图元组装与图元分配,这样就回剪裁出来视景体中的物体.但是如果我想在3D场景中规定一个区域,凡是在这个区域中的物体就能显示出来,非这 ...