zoukankan      html  css  js  c++  java
  • 深入.NET平台和C#编程.第五章:上机练习4

    ---------------------------------------------ChengFa类---------------------------------------------

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace JiSji
     8 {
     9     public class ChengFa : Operation
    10     {
    11         /// <summary>
    12         /// 乘法类
    13         /// </summary>
    14         /// <returns></returns>
    15         public override double GetResult()
    16         {
    17             double result = NumberA * NumberB;
    18             return result;
    19         }
    20     }
    21 
    22 
    23 }
    View Code

    ---------------------------------------------ChuFa类-----------------------------------------------

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace JiSji
     8 {
     9     /// <summary>
    10     /// 除法类
    11     /// </summary>
    12     public class ChuFa : Operation
    13     {
    14         public override double GetResult()
    15         {
    16             if (NumberB == 0)
    17             {
    18                 throw new Exception("除数不能为0!");
    19             }
    20             double result = NumberA / NumberB;
    21             return result;
    22         }
    23     }
    24 
    25 
    26 }
    View Code

    ---------------------------------------------formJiSji窗口-------------------------------------------

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Threading.Tasks;
     9 using System.Windows.Forms;
    10 
    11 namespace JiSji
    12 {
    13     public partial class formJiSji : Form
    14     {
    15         public formJiSji()
    16         {
    17             InitializeComponent();
    18         }
    19 
    20         /// <summary>
    21         /// 计算
    22         /// </summary>
    23         /// <param name="sender"></param>
    24         /// <param name="e"></param>
    25         private void button1_Click(object sender, EventArgs e)
    26         {
    27             if (string.IsNullOrEmpty(this.txtshu1.Text.Trim()) || string.IsNullOrEmpty(this.txtshu2.Text.Trim()))
    28             {
    29                 MessageBox.Show("操作数不能为空!");
    30                 this.txtshu1.Focus();
    31             }
    32             else
    33             {
    34                 try
    35                 {
    36                     Operation opr = new Operation();
    37                     switch (this.cmbfuh.SelectedItem.ToString().Trim())
    38                     {
    39                         case "+":
    40                             {
    41                                 opr = new JiaFa();
    42                             }
    43                             break;
    44                         case "-":
    45                             {
    46                                 opr = new JianFa(); 
    47                             }
    48                             break;
    49                         case "*":
    50                             {
    51                                 opr = new ChengFa();
    52                             }
    53                             break;
    54                         case "/":
    55                             {
    56                                 opr = new ChuFa();
    57                             }
    58                             break;
    59 
    60                         default:
    61                             break;
    62                     }
    63                     opr.NumberA = double.Parse(this.txtshu1.Text.Trim());
    64                     opr.NumberB = double.Parse(this.txtshu2.Text.Trim());
    65                     this.label2.Text = opr.GetResult().ToString();
    66                     this.label1.Visible = true;
    67                     this.label2.Visible = true;
    68                 }
    69                 catch (Exception ex)
    70                 {
    71                     MessageBox.Show("发生错误!" + ex.Message);
    72                 }
    73             }
    74         }
    75 
    76 
    77     }
    78 }
    View Code

    ---------------------------------------------JiaFa类-------------------------------------------------

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace JiSji
     8 {
     9     /// <summary>
    10     /// 加法类
    11     /// </summary>
    12     public class JiaFa : Operation
    13     {
    14         public override double GetResult()
    15         {
    16             double result = NumberA + NumberB;
    17             return result;
    18         }
    19     }
    20 
    21 
    22 }
    View Code

    ---------------------------------------------JianFa类------------------------------------------------

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace JiSji
     8 {
     9     /// <summary>
    10     /// 减法类型
    11     /// </summary>
    12     public class JianFa : Operation
    13     {
    14         public override double GetResult()
    15         {
    16             double result = NumberA - NumberB;
    17             return result;
    18         }
    19     }
    20 
    21 
    22 }
    View Code

    ---------------------------------------------Operation父类------------------------------------------

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace JiSji
     8 {
     9     /// <summary>
    10     /// 父类
    11     /// </summary>
    12     public class Operation
    13     {
    14         public double NumberA { get; set; }
    15         public double NumberB { get; set; }
    16         public virtual double GetResult()
    17         {
    18             double result = 0;
    19             return result;
    20         }
    21     }
    22 
    23 
    24 }
    View Code

    ---------------------------------------------运行结果------------------------------------------------

  • 相关阅读:
    【个人笔记】MySQL聚合函数、子查询
    artTemplate的使用总结
    【学习笔记】node.js入门基础
    【知了堂学习笔记】SQL查询基础语句(单表查询、多表查询)
    浅谈java final关键字
    3D魔幻旋转
    3分钟实现星空图
    深入浅出,谈谈面向对象几大特征
    html,css常用标签
    java中常用集合的理解
  • 原文地址:https://www.cnblogs.com/chenhui666/p/6691794.html
Copyright © 2011-2022 走看看