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();
//注册方法
p.shot += p.Gun;
//调用方法
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