using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TDelegate
{
public class Program
{
//定義委托
public delegate void knock(int mess);
//定義事件
public event knock shot;
public static void Main()
{
Program p = new Program();
//注冊(cè)方法
p.shot += p.Gun;
//調(diào)用方法
p.KnockDoor();
Console.ReadKey();
}
public void KnockDoor()
{
for (int i = 0; i < 3; i ++ )
{
if(i == 2)
{
if(shot != null)
{
shot(i);
}
}
}
}
//開槍的方法
public void Gun(int x)
{
Console.WriteLine("開門{0}次,兔兔 吐吐 突突,,,,開槍", x);
}
}
}
本文摘自 :https://blog.51cto.com/u