• 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;
    }
    }

    }

  • 相关阅读:
    hdu 3715(二分+2-sat)
    hdu 3622(二分+2-sat判断可行性)
    hdu 3062+1824(2-sat入门)
    【转载】lucene中Field.Index,Field.Store详解
    【转载】那些年我们一起清除过的浮动demo
    【转载】那些年我们一起清除过的浮动
    【转载】Visaul Studio 常用快捷键的动画演示
    【转载】各浏览器CSS兼容问题
    【转载】HTTP 错误 500.19
    【转载】Memcached在.Net中的基本操作
  • 原文地址:https://www.cnblogs.com/fly0512/p/9784328.html
Copyright © 2020-2023  润新知