• 委托(续2)


    ⑤手写标准事件

    老师留作业 学生做作业, 老师再留 学生咒骂

     1   public class CustomEvent
     2     {
     3         public static void  show() {
     4             student s = new student()
     5             {
     6                 id = 99,
     7                 Name = "小明"
     8             };
     9             Console.WriteLine($"{s.Name}的作业数量{s.HomeWorkCount}");
    10             s.studentHomeWorkToomuch += new TeacherInfo().addHomeWork;
    11             s.studentHomeWorkToomuch += new StudentInfo().doHomeWork;
    12             s.HomeWorkCount = 5;
    13             Console.WriteLine($"{s.Name}的作业数量{s.HomeWorkCount}");
    14 
    15 
    16 
    17         }
    18         /// <summary>
    19         /// 订户:关注事件,事件触发后,自己做出相应的动作
    20         /// </summary>
    21         public class StudentInfo {
    22             public void doHomeWork(object s,EventArgs e) {
    23                 Console.WriteLine("***********学生的反应********************");
    24                 student stu = (student)s;
    25                 HomeWorkEventArgs homeWorkEventArgs = (HomeWorkEventArgs)e;
    26                 Console.WriteLine($"学生姓名:{stu.Name}");
    27                 Console.WriteLine($"学生作业完成数量:{homeWorkEventArgs.finishedCont}");
    28                 Console.WriteLine($"学生未完成作业数量:{homeWorkEventArgs.WillfinishCont}");
    29                 Console.WriteLine($"{stu.Name} : 老师你睡了吗?!");
    30                 Console.WriteLine("*******************************");
    31 
    32             }
    33         }
    34         /// <summary>
    35         /// 订户:关注事件,事件触发后,自己做出相应的动作
    36         /// </summary>
    37         public class TeacherInfo
    38         {
    39             public void addHomeWork(object s, EventArgs e)
    40             {
    41                 Console.WriteLine("***********老师的反应********************");
    42                 Console.WriteLine("没有写完,放学别走");
    43                 Console.WriteLine("*******************************");
    44 
    45             }
    46         }
    47         /// <summary>
    48         /// 事件的发布者者,发布是按在满足的情况下,触发事件
    49         /// </summary>
    50         public class student
    51         {
    52             public int id { get; set; }
    53             public string Name { get; set; }
    54 
    55             private int _homeWorkCount=2;//可以完成数量
    56 
    57             public int HomeWorkCount
    58             {
    59                 get
    60                 {
    61                     return _homeWorkCount;
    62                 }
    63                 set
    64                 {
    65                     if (value > _homeWorkCount)
    66                     {
    67                         //触发
    68                         studentHomeWorkToomuch.Invoke(this, new HomeWorkEventArgs()
    69                         {
    70                             finishedCont = _homeWorkCount,
    71                             WillfinishCont = value - _homeWorkCount
    72                         });;
    73                     }
    74                     _homeWorkCount = value;
    75                 }
    76             }
    77             public event EventHandler studentHomeWorkToomuch;
    78         }
    79         public class HomeWorkEventArgs : EventArgs
    80         {
    81             public int finishedCont { get; set; }
    82             public int WillfinishCont { get; set; }
    83         }
    84 
    85 
    86     }
    View Code

    //ps:不知道我有没有写清需求^_^

  • 相关阅读:
    Mysql8.0中caching_sha2_password报错解决
    Centos7 安装mysql-8.0.18(rpm)
    如何有效的清理yum缓存
    虚拟机安装后配置IP地址
    正确计算linux系统内存使用率
    Linux 怎样更改locale语言设置
    linux把EDT时间修改为CST格式
    开放接口的安全验证方案(AES+RSA)
    Linux下JDK中文字体乱码
    服务器http请求https服务时报错解决方案
  • 原文地址:https://www.cnblogs.com/netCat/p/14981080.html
Copyright © 2020-2023  润新知