namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
Mum mum = new Mum();
Dad dad = new Dad();
Son son = new Son();
Sister sister = new Sister();
mum.TellEat();
mum.EatDel += son.SonEat;
mum.EatDel += dad.DadEat;
mum.EatDel += sister.SisterEat;
mum.EatDel();
//最后执行这个委托即可
Console.ReadKey();
}
}
//定义一个无参无返的委托
public delegate void HaveEatDel();
public class Mum
{
public void TellEat()
{
Console.WriteLine("吃饭了");
}
//定义委托实例
public HaveEatDel EatDel { get; set; }
}
public class Son
{
public void SonEat()
{
Console.WriteLine("知道了,玩完游戏再去");
}
}
public class Dad
{
public void DadEat()
{
Console.WriteLine("就来!");
}
}
public class Sister
{
public void SisterEat()
{
Console.WriteLine("一会就来");
}
}
}