c#学习笔记2-委托
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Demo
- {
- class Demo9
- {
- //static void Main(string[] args)
- //{
- // A a = new A();
- // B b = new B(a);
- // C c = new C(a);
- // a.Fall();
- // Console.ReadLine();
- //}
- }
- /// <summary>
- /// 首领A举杯委托
- /// </summary>
- /// <param name="hand">手:左、右</param>
- public delegate void RaiseEventHandler(string hand);
- /// <summary>
- /// 首领A摔杯委托
- /// </summary>
- public delegate void FallEventHandler();
- /// <summary>
- /// 首领A
- /// </summary>
- public class A
- {
- /// <summary>
- /// 首领A举杯事件
- /// </summary>
- public event RaiseEventHandler RaiseEvent;
- /// <summary>
- /// 首领A摔杯事件
- /// </summary>
- public event FallEventHandler FallEvent;
- /// <summary>
- /// 举杯
- /// </summary>
- /// <param name="hand">手:左、右</param>
- public void Raise(string hand)
- {
- Console.WriteLine("A{0} is ready", hand);
- // 调用举杯事件,传入左或右手作为参数
- if (RaiseEvent != null)
- {
- RaiseEvent(hand);
- }
- }
- /// <summary>
- /// 摔杯
- /// </summary>
- public void Fall()
- {
- Console.WriteLine("A is goods");
- // 调用摔杯事件
- if (FallEvent != null)
- {
- FallEvent();
- }
- }
- }
- /// <summary>
- /// 部下B
- /// </summary>
- public class B
- {
- A a;
- public B(A a)
- {
- this.a = a;
- a.RaiseEvent += new RaiseEventHandler(a_RaiseEvent); // 订阅举杯事件
- a.FallEvent += new FallEventHandler(a_FallEvent); // 订阅摔杯事件
- }
- /// <summary>
- /// 首领举杯时的动作
- /// </summary>
- /// <param name="hand">若首领A左手举杯,则B攻击</param>
- void a_RaiseEvent(string hand)
- {
- if (hand.Equals("left"))
- {
- Attack();
- }
- }
- /// <summary>
- /// 首领摔杯时的动作
- /// </summary>
- void a_FallEvent()
- {
- Attack();
- }
- /// <summary>
- /// 攻击
- /// </summary>
- public void Attack()
- {
- Console.WriteLine("b attack");
- }
- }
- /// <summary>
- /// 部下C
- /// </summary>
- public class C
- {
- A a;
- public C(A a)
- {
- this.a = a;
- a.RaiseEvent += new RaiseEventHandler(a_RaiseEvent); // 订阅举杯事件
- a.FallEvent += new FallEventHandler(a_FallEvent); // 订阅摔杯事件
- }
- /// <summary>
- /// 首领举杯时的动作
- /// </summary>
- /// <param name="hand">若首领A右手举杯,则攻击</param>
- void a_RaiseEvent(string hand)
- {
- if (hand.Equals("right"))
- {
- Attack();
- }
- }
- /// <summary>
- /// 首领摔杯时的动作
- /// </summary>
- void a_FallEvent()
- {
- Attack();
- }
- /// <summary>
- /// 攻击
- /// </summary>
- public void Attack()
- {
- Console.WriteLine("c attack");
- }
- }
- }
c#学习笔记2-委托的更多相关文章
- 《C# 语言学习笔记》——委托
委托是一种可以把引用存储为函数的类型. 委托的声明非常类似于函数,但不带函数体,且要使用delegate关键字.委托的声明制定了一个返回类型和一个参数列表. 在定义了委托后,就可以声明该委托类型的变量 ...
- [读书笔记]C#学习笔记二: 委托和事件的用法及不同.
前言: C#委托是什么 c#中的委托可以理解为函数的一个包装, 它使得C#中的函数可以作为参数来被传递, 这在作用上相当于C++中的函数指针. C++用函数指针获取函数的入口地址, 然后通过这个指针 ...
- [C#学习笔记]Func委托与Action委托
学习一项新知识的时候,最好的方法就是去实践它. 前言 <CLR via C#>这本神书真的是太有意思了!好的我的前言就是这个. Fun 如果要用有输入参数,有返回值的委托,那么Func委托 ...
- js学习笔记-事件委托
通过事件委托,你可以把事件处理器绑定到父元素上,避免了把事件处理器添加到多个子级元素上.从而优化性能. 事件代理用到了事件冒泡和目标元素.而任何一个元素的目标元素都是一开始的那个元素. 这里首先要注意 ...
- 【c# 学习笔记】委托链的使用
委托链其实就是委托类型,只是委托链把多个委托链接在一起而已,也就是说,我们把链接了多个方法的委托称为委托链或多路广播委托.如下: public delegate void DelegateTest() ...
- 【c# 学习笔记】委托的使用
//委托使用的演示 class Program { //1.使用delegate关键字来定义一个委托类型 public delegate void MyDelegate(int para1, int ...
- c#学习笔记之委托
委托 最近自己在调试C#项目,发现经常可以看到委托和lambda表达式,各种花里胡哨的写法把我给整的云里雾里的,于是自己特意花了一点功夫来整理关于delegate的相关知识,方便自己日后查阅. 何为委 ...
- c#学习笔记03——委托和事件
委托:一种引用类型,这种类型可以用来定义方法签名,从而使用委托实现将方法作为参数传递给其他方法.类似于C++中的函数之争,使用委托使程序员可以将方法引用封装在委托对象内. 定义和声明委托: deleg ...
- 委托学习笔记后续:泛型委托及委托中所涉及到匿名方法、Lambda表达式
引言: 最初学习c#时,感觉委托.事件这块很难,其中在学习的过程中还写了一篇学习笔记:委托.事件学习笔记.今天重新温故委托.事件,并且把最近学习到和委托相关的匿名方法.Lambda表达式及泛型委托记录 ...
- C#委托与事件学习笔记
委托事件学习笔记 本文是学习委托和事件的笔记,水平有限,如有错漏之处,还望大神不吝赐教. 什么是委托?从字面意思来解释,就是把一个动作交给别人去执行.在实际开发中最常用的就是使一个方法可以当做一个参数 ...
随机推荐
- Python -- seek定位文件指针位置 错误 io.UnsupportedOperation: can't do nonzero cur-relative seeks错误
f=open("E:/test/悯农.txt",'r') str=f.read(17) print("读取的数据是:",str) position=f.tell ...
- ent 基本使用五 schema介绍
ent 提供了自动生成schema 但是,我们可以基于生成schema 进行扩展,schema 主要包含以下配置 实体的字段(或者属性)比如 user 的name 以及age 实体的边(关系),比如u ...
- 信息学奥赛一本通 提高篇 序列第k个数 及 快速幂
我是传送门 这个题首先是先判断是等差还是等比数列 等差的话非常简单: 前后两个数是等差的,举个栗子: 3 6 9 12 这几个数,(我感觉 1 2 3 4并说明不了什么) 每次都加3嘛,很容易看出,第 ...
- Inno setup 判断系统32位还是64位
[Files] ; Install MyProg-x64.exe -bit mode (x64; see above), ; Check: Is64BitInstallMode 表示是64位windo ...
- MySQL数据库索引的底层原理(二叉树、平衡二叉树、B-Tree、B+Tree)
1.MySQL数据库索引的底层原理 https://mp.weixin.qq.com/s/zA9KvCkkte2mTWTcDv7hUg
- Loadrunner11压测过程问题总结
1.-27727: 下载资源时步骤下载超时 (120 seconds) 已过期 由于压力大了,下载资源所用时间就长了,可以设置加大超时时间: 运行时设置--Internet 协议--首选项--高级-- ...
- 统计git提交代码量
# a新增行数,d删除行数 git log --author="`git config --get user.name`" --pretty="%H" --a ...
- JS 从整数里 随机选一个
比如:现有数字随机一个 num = 3)) // 现有数随机一个 randomNum 的值只会是 0 1 2 3 里的随机一个 如果想要从数组随机一个下标index 就不要+1 如: parseInt ...
- mysql新增用户
新开了个项目,数据库也想新搞个用户,先登陆mysql,看看原来都有哪些: root@wlf:/# mysql -uroot -p Enter password: Welcome to the MySQ ...
- mysql插入报错:java.sql.SQLException: Incorrect string value: '\xE6\x9D\xAD\xE5\xB7\x9E...' for column 'address' at row 1
界面报错: 日志报错: java.sql.SQLException: Incorrect at com.mysql.cj.jdbc.exceptions.SQLError.createSQLExcep ...