(深入.Net平台和C#编程)第六章.上机练习4.20170410
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了(深入.Net平台和C#编程)第六章.上机练习4.20170410,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3611字,纯文字阅读大概需要6分钟。
内容图文
----------父类----------
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Lesson6_4 8 { 9 /// <summary> 10 /// 父类 11 /// </summary> 12 public class Operation 13 { 14 /// <summary> 15 /// 添加属性 16 /// </summary> 17 public double NumberA { get; set; } 1819publicdouble NumberB { get; set; } 20212223///<summary>24/// 定义虚方法GenResult(),返回类型为double 25///</summary>26///<returns></returns>27publicvirtualdouble GetResult() 28 { 29double result = 0; 30return result; 31 } 32 } 33 }
----------OperationAdd类----------
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Lesson6_4 8 { 9 /// <summary> 10 /// 实现加法类 11 /// </summary> 12 public class OperationAdd:Operation//调用父类13 { 14publicoverridedouble GetResult() 15 { 16double result = NumberA + NumberB; 17return result; 18 } 1920 } 2122///<summary>23/// 实现减法类 24///</summary>25publicclass OperationAn : Operation//调用父类26 { 27publicoverridedouble GetResult() 28 { 29double result = NumberA - NumberB; 30return result; 31 } 32 } 3334///<summary>35/// 实现除法的类 36///</summary>37publicclass OperationDiv : Operation 38 { 39publicoverridedouble GetResult() 40 { 41if (NumberB == 0) 42 { 43thrownew Exception("除数不能为0!"); 44 } 45double result = NumberA / NumberB; 46return result; 47 } 48 } 4950/// 实现乘法的类 51///</summary>52publicclass OperationCheng : Operation 53 { 54publicoverridedouble GetResult() 55 { 56if (NumberB == 0) 57 { 58thrownew Exception("除数不能为0!"); 59 } 60double result = NumberA * NumberB; 61return result; 62 } 63 } 64 }
----------FrmCalc类----------
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 Lesson6_4 12 { 13 public partial class FrmCalc : Form 14 { 15 public FrmCalc() 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//判断文本是否为空28if (string.IsNullOrEmpty(txtOne.Text.Trim()) || string.IsNullOrEmpty(txtTwo.Text.Trim())) 29 { 30 MessageBox.Show("操作数不能为空!", "提示"); 31//设置焦点32this.txtOne.Focus(); 33this.txtTwo.Focus(); 34return; 35 } 36else37 { 38//设置符号39try40 { 41 Operation opr = new Operation(); 42switch (this.cmdOper.SelectedItem.ToString().Trim()) 43 { 44case"+": 45 { 46 opr = new OperationAdd(); 47break; 48 } 49case"-": 50 { 51 opr = new OperationAn(); 52break; 53 } 54case"*": 55 { 56 opr = new OperationCheng(); 57break; 58 } 59case"/": 60 { 61 opr = new OperationDiv(); 62break; 63 } 64 } 65//计算参与计算的数据66 opr.NumberA = double.Parse(this.txtOne.Text.Trim()); 67 opr.NumberB = double.Parse(this.txtTwo.Text.Trim()); 68//计算69this.lbResult.Text = opr.GetResult().ToString(); 70this.lbResult.Visible = true; 71 } 72catch (Exception ex) 73 { 7475 MessageBox.Show("发生错误!" + ex.Message); 76 } 77 } 78 } 7980///<summary>81/// 设置组合框默认选中 82///</summary>83///<param name="sender"></param>84///<param name="e"></param>85privatevoid FrmCalc_Load(object sender, EventArgs e) 86 { 87 cmdOper.SelectedIndex = 0; 88 } 89 } 90 }
原文:http://www.cnblogs.com/weiai520/p/6691784.html
内容总结
以上是互联网集市为您收集整理的(深入.Net平台和C#编程)第六章.上机练习4.20170410全部内容,希望文章能够帮你解决(深入.Net平台和C#编程)第六章.上机练习4.20170410所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。