本帖最后由 夏闯富 于 2013-10-20 18:07 编辑
我用的是委托,下面是全部后台代码(用wpf做的)- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace 计算器
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public delegate int 委托(int F, int S);
- public partial class MainWindow : Window
- {
- 委托 a;
- int f;
-
- public MainWindow()
- {
- InitializeComponent();
- }
- private void _7_Click(object sender, RoutedEventArgs e)
- {
-
- Button button = (Button)sender;
- btn.Content = button.Content;
-
- }
- private void Button_Click_1(object sender, RoutedEventArgs e)
- {
- a = new 委托(运算方法.加法);
- f = Convert.ToInt32(btn.Content);
- }
- private void Button_Click_2(object sender, RoutedEventArgs e)
- {
- a = new 委托(运算方法.减法);
- f = Convert.ToInt32(btn.Content);
- }
- private void Button_Click_3(object sender, RoutedEventArgs e)
- {
- a = new 委托(运算方法.乘法);
- f = Convert.ToInt32(btn.Content);
- }
- private void Button_Click_4(object sender, RoutedEventArgs e)
- {
- a = new 委托(运算方法.除法);
- f = Convert.ToInt32(btn.Content);
- }
- private void Result(object sender, RoutedEventArgs e)
- {
- int R= a(f,Convert.ToInt32( btn.Content));
- btn.Content = R.ToString();
- }
- }
- public static class 运算方法
- {
- static public int 加法(int F,int S )
- {
- return F + S;
- }
- static public int 减法(int F, int S)
- {
- return F - S;
- }
- static public int 乘法(int F, int S)
- {
- return F * S;
- }
- static public int 除法(int F, int S)
- {
- return F / S;
- }
-
-
- }
- }
复制代码 |