1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Demo
  9. {
  10. class Demo9
  11. {
  12. //static void Main(string[] args)
  13. //{
  14.  
  15. // A a = new A();
  16. // B b = new B(a);
  17. // C c = new C(a);
  18. // a.Fall();
  19. // Console.ReadLine();
  20.  
  21. //}
  22. }
  23.  
  24. /// <summary>
  25. /// 首领A举杯委托
  26. /// </summary>
  27. /// <param name="hand">手:左、右</param>
  28. public delegate void RaiseEventHandler(string hand);
  29. /// <summary>
  30. /// 首领A摔杯委托
  31. /// </summary>
  32. public delegate void FallEventHandler();
  33. /// <summary>
  34. /// 首领A
  35. /// </summary>
  36. public class A
  37. {
  38. /// <summary>
  39. /// 首领A举杯事件
  40. /// </summary>
  41. public event RaiseEventHandler RaiseEvent;
  42. /// <summary>
  43. /// 首领A摔杯事件
  44. /// </summary>
  45. public event FallEventHandler FallEvent;
  46.  
  47. /// <summary>
  48. /// 举杯
  49. /// </summary>
  50. /// <param name="hand">手:左、右</param>
  51. public void Raise(string hand)
  52. {
  53. Console.WriteLine("A{0} is ready", hand);
  54. // 调用举杯事件,传入左或右手作为参数
  55. if (RaiseEvent != null)
  56. {
  57. RaiseEvent(hand);
  58. }
  59. }
  60. /// <summary>
  61. /// 摔杯
  62. /// </summary>
  63. public void Fall()
  64. {
  65. Console.WriteLine("A is goods");
  66. // 调用摔杯事件
  67. if (FallEvent != null)
  68. {
  69. FallEvent();
  70. }
  71. }
  72. }
  73. /// <summary>
  74. /// 部下B
  75. /// </summary>
  76. public class B
  77. {
  78. A a;
  79.  
  80. public B(A a)
  81. {
  82. this.a = a;
  83. a.RaiseEvent += new RaiseEventHandler(a_RaiseEvent); // 订阅举杯事件
  84. a.FallEvent += new FallEventHandler(a_FallEvent); // 订阅摔杯事件
  85. }
  86. /// <summary>
  87. /// 首领举杯时的动作
  88. /// </summary>
  89. /// <param name="hand">若首领A左手举杯,则B攻击</param>
  90. void a_RaiseEvent(string hand)
  91. {
  92. if (hand.Equals("left"))
  93. {
  94. Attack();
  95. }
  96. }
  97.  
  98. /// <summary>
  99. /// 首领摔杯时的动作
  100. /// </summary>
  101. void a_FallEvent()
  102. {
  103. Attack();
  104. }
  105.  
  106. /// <summary>
  107. /// 攻击
  108. /// </summary>
  109. public void Attack()
  110. {
  111. Console.WriteLine("b attack");
  112. }
  113. }
  114. /// <summary>
  115. /// 部下C
  116. /// </summary>
  117. public class C
  118. {
  119. A a;
  120. public C(A a)
  121. {
  122. this.a = a;
  123. a.RaiseEvent += new RaiseEventHandler(a_RaiseEvent); // 订阅举杯事件
  124. a.FallEvent += new FallEventHandler(a_FallEvent); // 订阅摔杯事件
  125. }
  126. /// <summary>
  127. /// 首领举杯时的动作
  128. /// </summary>
  129. /// <param name="hand">若首领A右手举杯,则攻击</param>
  130. void a_RaiseEvent(string hand)
  131. {
  132. if (hand.Equals("right"))
  133. {
  134. Attack();
  135. }
  136. }
  137.  
  138. /// <summary>
  139. /// 首领摔杯时的动作
  140. /// </summary>
  141. void a_FallEvent()
  142. {
  143. Attack();
  144. }
  145. /// <summary>
  146. /// 攻击
  147. /// </summary>
  148. public void Attack()
  149. {
  150. Console.WriteLine("c attack");
  151. }
  152. }
  153. }

  

c#学习笔记2-委托的更多相关文章

  1. 《C# 语言学习笔记》——委托

    委托是一种可以把引用存储为函数的类型. 委托的声明非常类似于函数,但不带函数体,且要使用delegate关键字.委托的声明制定了一个返回类型和一个参数列表. 在定义了委托后,就可以声明该委托类型的变量 ...

  2. [读书笔记]C#学习笔记二: 委托和事件的用法及不同.

    前言:  C#委托是什么 c#中的委托可以理解为函数的一个包装, 它使得C#中的函数可以作为参数来被传递, 这在作用上相当于C++中的函数指针. C++用函数指针获取函数的入口地址, 然后通过这个指针 ...

  3. [C#学习笔记]Func委托与Action委托

    学习一项新知识的时候,最好的方法就是去实践它. 前言 <CLR via C#>这本神书真的是太有意思了!好的我的前言就是这个. Fun 如果要用有输入参数,有返回值的委托,那么Func委托 ...

  4. js学习笔记-事件委托

    通过事件委托,你可以把事件处理器绑定到父元素上,避免了把事件处理器添加到多个子级元素上.从而优化性能. 事件代理用到了事件冒泡和目标元素.而任何一个元素的目标元素都是一开始的那个元素. 这里首先要注意 ...

  5. 【c# 学习笔记】委托链的使用

    委托链其实就是委托类型,只是委托链把多个委托链接在一起而已,也就是说,我们把链接了多个方法的委托称为委托链或多路广播委托.如下: public delegate void DelegateTest() ...

  6. 【c# 学习笔记】委托的使用

    //委托使用的演示 class Program { //1.使用delegate关键字来定义一个委托类型 public delegate void MyDelegate(int para1, int ...

  7. c#学习笔记之委托

    委托 最近自己在调试C#项目,发现经常可以看到委托和lambda表达式,各种花里胡哨的写法把我给整的云里雾里的,于是自己特意花了一点功夫来整理关于delegate的相关知识,方便自己日后查阅. 何为委 ...

  8. c#学习笔记03——委托和事件

    委托:一种引用类型,这种类型可以用来定义方法签名,从而使用委托实现将方法作为参数传递给其他方法.类似于C++中的函数之争,使用委托使程序员可以将方法引用封装在委托对象内. 定义和声明委托: deleg ...

  9. 委托学习笔记后续:泛型委托及委托中所涉及到匿名方法、Lambda表达式

    引言: 最初学习c#时,感觉委托.事件这块很难,其中在学习的过程中还写了一篇学习笔记:委托.事件学习笔记.今天重新温故委托.事件,并且把最近学习到和委托相关的匿名方法.Lambda表达式及泛型委托记录 ...

  10. C#委托与事件学习笔记

    委托事件学习笔记 本文是学习委托和事件的笔记,水平有限,如有错漏之处,还望大神不吝赐教. 什么是委托?从字面意思来解释,就是把一个动作交给别人去执行.在实际开发中最常用的就是使一个方法可以当做一个参数 ...

随机推荐

  1. 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 ...

  2. ent 基本使用五 schema介绍

    ent 提供了自动生成schema 但是,我们可以基于生成schema 进行扩展,schema 主要包含以下配置 实体的字段(或者属性)比如 user 的name 以及age 实体的边(关系),比如u ...

  3. 信息学奥赛一本通 提高篇 序列第k个数 及 快速幂

    我是传送门 这个题首先是先判断是等差还是等比数列 等差的话非常简单: 前后两个数是等差的,举个栗子: 3 6 9 12 这几个数,(我感觉 1 2 3 4并说明不了什么) 每次都加3嘛,很容易看出,第 ...

  4. Inno setup 判断系统32位还是64位

    [Files] ; Install MyProg-x64.exe -bit mode (x64; see above), ; Check: Is64BitInstallMode 表示是64位windo ...

  5. MySQL数据库索引的底层原理(二叉树、平衡二叉树、B-Tree、B+Tree)

    1.MySQL数据库索引的底层原理 https://mp.weixin.qq.com/s/zA9KvCkkte2mTWTcDv7hUg

  6. Loadrunner11压测过程问题总结

    1.-27727: 下载资源时步骤下载超时 (120 seconds) 已过期 由于压力大了,下载资源所用时间就长了,可以设置加大超时时间: 运行时设置--Internet 协议--首选项--高级-- ...

  7. 统计git提交代码量

    # a新增行数,d删除行数 git log  --author="`git config --get user.name`" --pretty="%H" --a ...

  8. JS 从整数里 随机选一个

    比如:现有数字随机一个 num = 3)) // 现有数随机一个 randomNum 的值只会是 0 1 2 3 里的随机一个 如果想要从数组随机一个下标index 就不要+1 如: parseInt ...

  9. mysql新增用户

    新开了个项目,数据库也想新搞个用户,先登陆mysql,看看原来都有哪些: root@wlf:/# mysql -uroot -p Enter password: Welcome to the MySQ ...

  10. 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 ...