C#委托与回调的应用体会(一)

在实现定积分通用函数计算程序设计过程中,第一次使用C#的委托特性,个人感觉C#的委托有点类似指针,就是把一个方法做成地址传递过去。尽管这种理解不一定严谨,但是取得的效果确实差不多。

以下便是委托的实际应用~

namespace functionTheOne
{
public partial class Form1 : Form
{
private delegate double Function(double x);
private Function function;
public Form1()
{
InitializeComponent();
}

private void label1_Click(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
if (radioButton3.Checked == true)
{
double a; double b; double z;
a = Convert.ToDouble(textBox1.Text);
b = Convert.ToDouble(textBox2.Text);
function = new Function(jishuan1);
hanshu(a, b, function);
}
if (radioButton2.Checked == true)
{
double a; double b; double z;
a = Convert.ToDouble(textBox1.Text);
b = Convert.ToDouble(textBox2.Text);
function = new Function(jishuan2);
hanshu(a, b, function);
}
}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{

}

private void label3_Click(object sender, EventArgs e)
{

}

private void Form1_Load(object sender, EventArgs e)
{

}

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{

}

private void radioButton2_CheckedChanged(object sender, EventArgs e)
{

}
private double hanshu(double a,double b,Function ch)//计算通用的函数
{
double sum=0;
for(int i = 0; i < 10000; i++)
{
sum = sum + (a-b)/10000*(ch(((a - b) / 10000 )*i+b)); //a是上限,b是下限
}
textBox3.Text = Convert.ToString(sum);
return sum;
}
private double jishuan1(double x )
{
return x;
}
private double jishuan2(double x)
{
return x * x;
}
}

}

posted @ 2018-10-13 21:39  诗酒-趁年华  阅读(238)  评论(0编辑  收藏  举报