C#——窗体程序利用委托进行两个数的加减乘除计算的多种方法。(委托的多种使用方法)

一 设计界面

二 编写代码

1 委托的第一种方法(声明 实例化 使用 )

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace 计算

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        public delegate int Calculate(int x, int y);  //声明委托——1

        int Add(int x, int y) { return x + y; }//加、减、乘、除的方法——2

        int Minus(int x, int y) { return x - y; }

        int Multiply(int x, int y) { return x * y; }

        int Divide(int x, int y) { return x / y; }

 

        private void button1_Click(object sender, EventArgs e)

        {

            Calculate a = new Calculate(Add);  //通过Calculate型的委托对象a来调用方法Add——3

            Calculate b = new Calculate(Minus);

            Calculate c = new Calculate(Multiply);//3

            Calculate d = new Calculate(Divide);

            int result1 = a(Convert.ToInt32(textBox1 .Text),Convert .ToInt32(textBox2 .Text)); //返回值赋给变量result1——4

            label2.Text = "" + result1;  //显示结果——5

            int result2 = b(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text));

            label2.Text += "\n\n" + result2;

            int result3 = c(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text));

            label2.Text += "\n\n" + result3;

            int result4 = d(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text));

            label2.Text += "\n\n" + result4;

        }

    }

}

 

 

2 委托的第二种 第三种方法(匿名方法+Lambda表达式)

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace 计算

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        public delegate int Calculate(int x, int y);  //声明委托——1      

        public Calculate handler;  //定义委托型的字段——2

 

        private void button1_Click(object sender, EventArgs e)

        {

            int a = Convert.ToInt32(textBox1 .Text);

            int b = Convert.ToInt32(textBox2 .Text);

 

            //Lambda表达式—— (1)

            handler = (x, y) => x + y;  //创建委托对象同时封装方法——3

 

            //匿名方法——(2)

            // handler = delegate(int x, int y) { return x + y; };

 

            label2.Text = handler(a, b).ToString();//通过委托对象调用方法——4         

 

            handler = (x, y) => x - y;

            label2.Text += "\n\n" + handler(a,b);

            handler = (x, y) => x * y;

            label2.Text += "\n\n" + handler(a, b);

            handler = (x, y) => x / y;

            label2.Text += "\n\n" + handler(a, b);

 

        }

    }

}

 

3 委托的第三种方法

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace 计算

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        public delegate void Calculate(int x, int y);  //声明委托——1

        public Calculate handler;  //定义委托型的字段——2

        public void Add(int x, int y) { label2.Text = (x + y).ToString(); }

        public void Mul(int x, int y) { label2.Text += "\n\n" + (x * y); }

        public void Sub(int x, int y) { label2.Text += "\n\n" + (x - y); }

        public void Div(int x, int y) { label2.Text += "\n\n" + (x / y); }

        private void button1_Click(object sender, EventArgs e)

        {

            int a = Convert.ToInt32(textBox1 .Text);

            int b = Convert.ToInt32(textBox2 .Text);

            handler = new Calculate(Add);  //创建委托对象同时封装方法——3

            handler += new Calculate(Sub);

            handler += new Calculate(Mul);

            handler += new Calculate(Div); //通过委托对象调用方法——4

            handler(a, b);

        }

    }

}

 

三 运行结果

  • 6
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值