C#委托、类和事件的验证

时间: 1ms        内存:128M

描述:

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

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);
            }
        }
    }
}

输入:

无输入

输出:

验证事件输出

示例输入:

示例输出:

teacher
student

提示:

参考答案(内存最优[0]):

using System;
namespace HelloWorldApplication
{
    public delegate void DelegateRing();
    public class Bell{
        public event DelegateRing Ring;
        public void OnRing(){ Ring(); }
    }
public class Teacher{
        public void Register(Bell bell){
            bell.Ring += new DelegateRing(HandleEvent);
            bell.OnRing();
        }
        public void HandleEvent(){Console.WriteLine("teacher");}
    }
    public 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);
            }
        }
    }
}

参考答案(时间最优[0]):

using System;
namespace HelloWorldApplication
{
    public delegate void DelegateRing();
    public class Bell{
        public event DelegateRing Ring;
        public void OnRing(){ Ring(); }
    }
public class Teacher{
        public void Register(Bell bell){
            bell.Ring += new DelegateRing(HandleEvent);
            bell.OnRing();
        }
        public void HandleEvent(){Console.WriteLine("teacher");}
    }
    public 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);
            }
        }
    }
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。