• 问题 C: C#委托、类和事件的验证


    题目描述

    程序由两部分组成,如下代码所示。第一部分定义了委托、类和事件。第二部分进行验证。 

    using System;
    namespace HelloWorldApplication
    {
        public delegate void DelegateRing();
        public class Bell{
            public event DelegateRing Ring;
            public void OnRing(){ Ring(); }
        }
    /////////////////////////////////////////////////////////////////
                
                请填写代码
    
    /////////////////////////////////////////////////////////////////
        class HelloWorld
        {
            static void Main(string[] args)
            {
                try{
                    Teacher teacher = new Teacher();
                    teacher.Register(new Bell());
                    Student student = new Student();
                    student.Register(new Bell());
                    Console.ReadKey();
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
    }

    输入

    无输入

    输出

    验证事件输出

    样例输入

    .wrapper {position: relative;} #input {position: absolute;top: 0;left: 0;opacity: 0;z-index: -10;}

    样例输出

    teacher
    student

    提示

    只需要输出样例输出的结果

    只需要提交需要填写的代码

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace 委托_类和事件的验证
    {
        public delegate void DelegateRing();
        public class Bell
        {
            public event DelegateRing Ring;
            public void OnRing() { Ring(); }
        }
    
        class Teacher
        {
            public void Register(Bell bell)
            {
                bell.Ring += new DelegateRing(HandleEvent);
                bell.OnRing();
            }
            public void HandleEvent()
            {
                Console.WriteLine("teacher");
            }
        }
        class Student
        {
            public void Register(Bell bell)
            {
                bell.Ring += new DelegateRing(HandleEvent2);
                bell.OnRing();
            }
            public void HandleEvent2()
            {
                Console.WriteLine("student");
            }
        }
    
        class HelloWorld
        {
            static void Main(string[] args)
            {
                try
                {
                    Teacher teacher = new Teacher();
                    teacher.Register(new Bell());
                    Student student = new Student();
                    student.Register(new Bell());
                    Console.ReadKey();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
    }
    

      

  • 相关阅读:
    C# 关于委托和事件的妙文:通过一个例子详细介绍委托和事件的作用;Observer模式简介
    Path.Combine (合并两个路径字符串)方法的一些使用细节
    taskkill /f /im的应用
    powersheel远程连接方法操作
    Centos 定时任务发送smtp邮件
    Centos 发送smtp邮件
    在 Windows 上安装Rabbit MQ 指南
    Quartz.NET总结(五)基于Quartz.net 的开源任务管理平台
    Quartz.NET总结(四)Quartz 远程调度
    Quartz.NET总结(三)Quartz 配置
  • 原文地址:https://www.cnblogs.com/mjn1/p/12619062.html
Copyright © 2020-2023  润新知