Descripción del Título
El programa consta de dos partes, como se muestra en el siguiente código. La primera parte define delegados, clases y eventos. La segunda parte es para verificar.
usando el sistema; espacio de nombres HelloWorldApplication { delegado público void DelegateRing (); public class Bell { evento público DelegateRing Ring; public void OnRing () {Ring (); } } / 请 填写 代码 / class HelloWorld { static void Main (string [] args) { try { Teacher teacher = new Teacher (); maestro.Registro (nuevo Bell ()); Estudiante estudiante = nuevo Estudiante (); estudiante.Registro (nuevo Bell ()); Console.ReadKey (); } catch (Exception ex) { Console.WriteLine (por ejemplo, mensaje); } } } }
ingresar
Sin entrada
Producción
Verificar la salida del evento
Entrada de muestra
no
Salida de muestra
alumno maestro
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.IO;
namespace ConsoleApplication1
{
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 Program
{
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);
}
}
}
}