24.委托(1)
1. 简单的delegate(小王吃西瓜... ...)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace _24.委托_1_
{
delegate void Eat(string food);
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static void xwEat(string food)
{
MessageBox.Show("小王吃" + food);
}
private void Form1_Load(object sender, EventArgs e)
{
Eat xw = new Eat(xwEat);
xw("西瓜");
// xwEat("西瓜");
}
}
}
2. 接着上面的继续扩展
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace _24.委托_1_
{
delegate void Eat(string food);
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static void xwEat(string food)
{
MessageBox.Show("小王吃" + food);
}
static void xlEat(string food)
{
MessageBox.Show("小李吃" + food);
}
static void xzEat(string food)
{
MessageBox.Show("小赵吃" + food);
}
private void Form1_Load(object sender, EventArgs e)
{
Eat xw = new Eat(xwEat);
Eat xl = new Eat(xlEat);
Eat xz = new Eat(xzEat);
xw("西瓜");
xl("西瓜");
xz("西瓜");
// xwEat("西瓜");
}
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace _24.委托_1_
{
delegate void Eat(string food);
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static void xwEat(string food)
{
MessageBox.Show("小王吃" + food);
}
static void xlEat(string food)
{
MessageBox.Show("小李吃" + food);
}
static void xzEat(string food)
{
MessageBox.Show("小赵吃" + food);
}
private void Form1_Load(object sender, EventArgs e)
{
Eat xw = new Eat(xwEat);
Eat xl = new Eat(xlEat);
Eat xz = new Eat(xzEat);
Eat EatChain;
EatChain = xw + xl + xz;
EatChain("西瓜");
}
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace _24.委托_1_
{
delegate void Eat(string food);
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static void xwEat(string food)
{
MessageBox.Show("小王吃" + food);
}
static void xlEat(string food)
{
MessageBox.Show("小李吃" + food);
}
static void xzEat(string food)
{
MessageBox.Show("小赵吃" + food);
}
private void Form1_Load(object sender, EventArgs e)
{
Eat xw = new Eat(xwEat);
Eat xl = new Eat(xlEat);
Eat xz = new Eat(xzEat);
Eat EatChain;
MessageBox.Show("三人开会");
EatChain = xw + xl + xz;
EatChain("西瓜");
MessageBox.Show("小王出去接电话");
EatChain -= xw;
EatChain("葡萄 ");
MessageBox.Show("小王回来和大家一起吃橘子");
EatChain += xw;
EatChain("橘子");
}
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace _24.委托_1_
{
delegate void Eat(string food);
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Eat eatChain = null;
eatChain += delegate(string food)
{
MessageBox.Show("小王吃" + food);
};
eatChain += delegate(string food)
{
MessageBox.Show("小赵吃" + food);
};
eatChain("西瓜");
}
}
}