1 namespace ConsoleApp4 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 Mum mum = new Mum(); 8 Dad dad = new Dad(); 9 ... ...
1 namespace ConsoleApp4 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 Mum mum = new Mum(); 8 Dad dad = new Dad(); 9 Son son = new Son(); 10 Sister sister = new Sister(); 11 mum.TellEat(); 12 mum.EatDel += son.SonEat; 13 mum.EatDel += dad.DadEat; 14 mum.EatDel += sister.SisterEat; 15 mum.EatDel(); 16 //最後執行這個委托即可 17 Console.ReadKey(); 18 } 19 } 20 //定義一個無參無返的委托 21 public delegate void HaveEatDel(); 22 public class Mum 23 { 24 public void TellEat() 25 { 26 Console.WriteLine("吃飯了"); 27 } 28 //定義委托實例 29 public HaveEatDel EatDel { get; set; } 30 } 31 public class Son 32 { 33 public void SonEat() 34 { 35 Console.WriteLine("知道了,玩完游戲再去"); 36 } 37 } 38 public class Dad 39 { 40 public void DadEat() 41 { 42 Console.WriteLine("就來!"); 43 } 44 } 45 public class Sister 46 { 47 public void SisterEat() 48 { 49 Console.WriteLine("一會就來"); 50 } 51 } 52 }