internal static void EventTest() { Console.WriteLine("事件示例\n"); NewsPublisher publisher = new NewsPublisher("新闻发布者"); NewsReader reader1 = new NewsReader("读者1", publisher); NewsReader reader2 = new NewsReader("读者2", publisher); NewsReader reader3 = new NewsReader("读者3", publisher); Console.WriteLine($"{publisher.Name}发布了一则新闻。"); publisher.PublishNews("早间新闻"); Console.WriteLine($"{publisher.Name}发布了一则新闻。"); publisher.PublishNews("晚间新闻"); }
//在构造函数中订阅一个事件 public NewsReader(string name, NewsPublisher publisher) { Name = name; publisher.NewsEvent += HandleNewsEvent; }