using
System;
class Publisher // 出版社
... {
public delegate void PubComputer(string magazineName);//声明发布电脑杂志事件所需的代理
public delegate void PubLife(string magazineName);//声明发布生活杂志事件所需的代理
public event PubComputer OnPubComputer;//事件的声明
public event PubLife OnPubLife;//事件的声明
public void issueComputer()//触发事件的方法
...{
if (OnPubComputer != null)
...{
Console.WriteLine("发行<<电脑杂志>>");
OnPubComputer("电脑杂志");
}
}
public void issueLife()
...{
if (OnPubLife != null)
...{
Console.WriteLine("发行<<生活杂志>>");
OnPubLife("生活杂志");
}
}
}
class Subscriber
... {
private string name;
public Subscriber(string name)
...{
this.name = name;
}
public void Receive(string magazineName)
...{
Console.WriteLine(name + "已经收到了" + magazineName);
}
}
class Story
... {
static void Main()
...{
Publisher Pub = new Publisher();
Subscriber zs = new Subscriber("张三");
Pub.OnPubComputer += new Publisher.PubComputer(zs.Receive);
Subscriber ls = new Subscriber("李四");
Pub.OnPubComputer += new Publisher.PubComputer(ls.Receive);
Pub.OnPubLife += new Publisher.PubLife(ls.Receive);
Pub.issueComputer();
Pub.issueLife();
Console.WriteLine();
Console.WriteLine("一年后");
Pub.OnPubComputer -= new Publisher.PubComputer(ls.Receive);
Pub.issueComputer();
Pub.issueLife();
}
}
class Publisher // 出版社
... {
public delegate void PubComputer(string magazineName);//声明发布电脑杂志事件所需的代理
public delegate void PubLife(string magazineName);//声明发布生活杂志事件所需的代理
public event PubComputer OnPubComputer;//事件的声明
public event PubLife OnPubLife;//事件的声明
public void issueComputer()//触发事件的方法
...{
if (OnPubComputer != null)
...{
Console.WriteLine("发行<<电脑杂志>>");
OnPubComputer("电脑杂志");
}
}
public void issueLife()
...{
if (OnPubLife != null)
...{
Console.WriteLine("发行<<生活杂志>>");
OnPubLife("生活杂志");
}
}
}
class Subscriber
... {
private string name;
public Subscriber(string name)
...{
this.name = name;
}
public void Receive(string magazineName)
...{
Console.WriteLine(name + "已经收到了" + magazineName);
}
}
class Story
... {
static void Main()
...{
Publisher Pub = new Publisher();
Subscriber zs = new Subscriber("张三");
Pub.OnPubComputer += new Publisher.PubComputer(zs.Receive);
Subscriber ls = new Subscriber("李四");
Pub.OnPubComputer += new Publisher.PubComputer(ls.Receive);
Pub.OnPubLife += new Publisher.PubLife(ls.Receive);
Pub.issueComputer();
Pub.issueLife();
Console.WriteLine();
Console.WriteLine("一年后");
Pub.OnPubComputer -= new Publisher.PubComputer(ls.Receive);
Pub.issueComputer();
Pub.issueLife();
}
}