现在的位置: 首页 > 综合 > 正文

24.委托(1)

2013年01月26日 ⁄ 综合 ⁄ 共 2116字 ⁄ 字号 评论关闭

1. 简单的delegate(小王吃西瓜... ...)

 

代码

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. 接着上面的继续扩展

代码

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("西瓜");
}
}

}

3. 接着上面的继续扩展

代码

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(
"西瓜");

}
}

}

4. 接着上面的继续扩展

代码

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(
"橘子");

}
}

}

5. 接着上面的继续扩展

代码

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(
"西瓜");

}
}

}

 

 

抱歉!评论已关闭.