今天在博客园上看到一道面试题,是关于委托和事件的,为了重温事件的应用,自己又做了一遍。
题目:
程序设计: 猫大叫一声,所有的老鼠都开始逃跑,主人被惊醒。(C#语言)
题目:
程序设计: 猫大叫一声,所有的老鼠都开始逃跑,主人被惊醒。(C#语言)
要求: 1.要有联动性,老鼠和主人的行为是被动的。
2.考虑可扩展性,猫的叫声可能引起其他联动效应。
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Cat cat = new Cat("阿甲");
Mouse mouse = new Mouse("阿乙");
Person person = new Person("阿丙");
Chicken chicken = new Chicken("阿丁");
cat.Cry();
Console.ReadKey();
}
}
public class ActionEventArgs : EventArgs
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
class Cat
{
public delegate void DeleAction(object sender,ActionEventArgs e);
public static event DeleAction ActionHandle;
private string name;
public string Name
{
set { name = value; }
get { return name; }
}
public Cat(string name)
{
this.name = name;
}
public void Cry()
{
Console.WriteLine(Name+"猫大叫一声!");
ActionEventArgs ae = new ActionEventArgs();
ae.Name = name;
OnAction(this,ae);
}
public void OnAction(object sender,ActionEventArgs e)
{
if (ActionHandle != null)
{
ActionHandle(sender, e);
}
}
}
class Mouse
{
private string name;
public string Name
{
set { name = value; }
get { return name; }
}
public Mouse(string name)
{
Cat.ActionHandle += new Cat.DeleAction(Action);
this.name = name;
}
public void Action(object sender,ActionEventArgs e)
{
Console.WriteLine(e.Name+"的叫声让老鼠"+this.Name+"四处逃窜!");
}
}
class Person
{
private string name;
public string Name
{
set { name = value; }
get { return name; }
}
public Person(string name)
{
Cat.ActionHandle += new Cat.DeleAction(Action);
this.name = name;
}
public void Action(object sender, ActionEventArgs e)
{
Console.WriteLine(e.Name + "的叫声让主人" + this.Name + "惊醒了!");
}
}
class Chicken
{
private string name;
public string Name
{
set { name = value; }
get { return name; }
}
public Chicken(string name)
{
Cat.ActionHandle += new Cat.DeleAction(Action);
this.name = name;
}
public void Action(object sender, ActionEventArgs e)
{
Console.WriteLine(e.Name + "的叫声让鸡" + this.Name + "也打鸣了!");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Cat cat = new Cat("阿甲");
Mouse mouse = new Mouse("阿乙");
Person person = new Person("阿丙");
Chicken chicken = new Chicken("阿丁");
cat.Cry();
Console.ReadKey();
}
}
public class ActionEventArgs : EventArgs
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
class Cat
{
public delegate void DeleAction(object sender,ActionEventArgs e);
public static event DeleAction ActionHandle;
private string name;
public string Name
{
set { name = value; }
get { return name; }
}
public Cat(string name)
{
this.name = name;
}
public void Cry()
{
Console.WriteLine(Name+"猫大叫一声!");
ActionEventArgs ae = new ActionEventArgs();
ae.Name = name;
OnAction(this,ae);
}
public void OnAction(object sender,ActionEventArgs e)
{
if (ActionHandle != null)
{
ActionHandle(sender, e);
}
}
}
class Mouse
{
private string name;
public string Name
{
set { name = value; }
get { return name; }
}
public Mouse(string name)
{
Cat.ActionHandle += new Cat.DeleAction(Action);
this.name = name;
}
public void Action(object sender,ActionEventArgs e)
{
Console.WriteLine(e.Name+"的叫声让老鼠"+this.Name+"四处逃窜!");
}
}
class Person
{
private string name;
public string Name
{
set { name = value; }
get { return name; }
}
public Person(string name)
{
Cat.ActionHandle += new Cat.DeleAction(Action);
this.name = name;
}
public void Action(object sender, ActionEventArgs e)
{
Console.WriteLine(e.Name + "的叫声让主人" + this.Name + "惊醒了!");
}
}
class Chicken
{
private string name;
public string Name
{
set { name = value; }
get { return name; }
}
public Chicken(string name)
{
Cat.ActionHandle += new Cat.DeleAction(Action);
this.name = name;
}
public void Action(object sender, ActionEventArgs e)
{
Console.WriteLine(e.Name + "的叫声让鸡" + this.Name + "也打鸣了!");
}
}
}