委托
image.png
声明委托
image.png
Action和Func
image.png
Lambda
image.png
image.png
事件及事件的应用
image.png
class Program
{
static void Main(string[] args)
{
Cat cat = new Cat("汤姆", 50);
Mouse mouse1 = new Mouse("杰瑞", 10,cat);
Mouse mouse2 = new Mouse("瑞士", 10,cat);
cat.Caught();
Console.ReadLine();
}
}
class Cat
{
string name;
int age;
public Cat(string name, int age)
{
this.name = name;
this.age = age;
}
public void Caught()
{
Console.WriteLine(name + "猫来抓老鼠了");
if (catCome != null)
catCome();
}
public event Action catCome;
}
class Mouse
{
string name;
int age;
public Mouse(string name, int age,Cat cat)
{
this.name = name;
this.age = age;
cat.catCome += Run;
}
public void Run()
{
Console.WriteLine(name + "快跑");
}
}
事件与委托的区别
网友评论