C#封装类并因此设计一个简易计算器

目录

一、涉及到的知识点

1.封装

2. 封装性的使用范围

二、实例

1.源码

2.生成效果


一、涉及到的知识点

1.封装

        面向对象编程中,大多数都是以类作为数据封装的基本单位。类将数据和操作数据的方法结合成一个单位。设计类时,不希望直接存取类中的数据,而是希望通过方法来存取数据,这样就可以达到封装数据的目的,方便以后的维护升级,也可以在操作数据时多一层判断。

        此外,封装还可以解决数据存取的权限问题,可以使用封装将数据隐藏起来,形成一个封闭的空间,然后设置哪些数据只能在这个空间中使用,哪些数据可以在空间外部使用。如果一个类中包含敏感数据,有些人可以访问,有些人不能访问,如果不对这些数据的访问加以限制,后果将会非常严重。所以在编写程序时,要对类的成员使用不同的访问修饰符,从而定义它们的访问级别。

        C#中可以使用类来达到数据封装的效果,这样就可以使数据与方法封装成单一元素,以便于通过方法存取数据。

2. 封装性的使用范围

        封装性是面向对象编程中最基本的一个特性,在C#中使用封装性时,主要是针对接口和类来说的。对于一些程序中通用的属性、方法等,通常都封装到接口或者类中,从而提高代码的重用率。

二、实例

        使用面向对象编程思想中的封装性编写一个简单的计算器。

1.源码

//封装类并因此设计一个计算器
namespace _114
{public partial class Form1 : Form{private TextBox? textBox1;private GroupBox? groupBox1;private GroupBox? groupBox2;private Button? button6;private Button? button5;private Button? button4;private Button? button3;private Button? button2;private Button? button1;private Button? button10;private Button? button9;private Button? button8;private Button? button7;private Button? button16;private Button? button14;private Button? button15;private Button? button11;private Button? button12;private Button? button13;int i = 0;//记录第一个数int j = 0;//记录第二个数string type = "";//记录运算类型public Form1(){InitializeComponent();StartPosition = FormStartPosition.CenterScreen;Load += Form1_Load;}private void Form1_Load(object? sender, EventArgs e){// // button16// button16 = new Button{Location = new Point(6, 112),Name = "button16",Size = new Size(112, 23),TabIndex = 5,Text = "0",UseVisualStyleBackColor = true};button16.Click += Button16_Click;// // button14// button14 = new Button{Location = new Point(48, 83),Name = "button14",Size = new Size(32, 23),TabIndex = 2,Text = "8",UseVisualStyleBackColor = true};button14.Click += Button14_Click;// // button15// button15 = new Button{Location = new Point(86, 83),Name = "button15",Size = new Size(32, 23),TabIndex = 3,Text = "9",UseVisualStyleBackColor = true};button15.Click += Button15_Click;// // button11// button11 = new Button{Location = new Point(48, 54),Name = "button11",Size = new Size(32, 23),TabIndex = 2,Text = "5",UseVisualStyleBackColor = true};button11.Click += Button11_Click;// // button12// button12 = new Button{Location = new Point(86, 54),Name = "button12",Size = new Size(32, 23),TabIndex = 3,Text = "6",UseVisualStyleBackColor = true};button12.Click += Button12_Click;// // button13// button13 = new Button{Location = new Point(6, 83),Name = "button13",Size = new Size(32, 23),TabIndex = 4,Text = "7",UseVisualStyleBackColor = true};button13.Click += Button13_Click;// // button10// button10 = new Button{Location = new Point(6, 54),Name = "button10",Size = new Size(32, 23),TabIndex = 3,Text = "4",UseVisualStyleBackColor = true};button10.Click += Button10_Click;// // button9// button9 = new Button{Location = new Point(88, 25),Name = "button9",Size = new Size(32, 23),TabIndex = 2,Text = "3",UseVisualStyleBackColor = true};button9.Click += Button9_Click;// // button8// button8 = new Button{Location = new Point(48, 25),Name = "button8",Size = new Size(30, 23),TabIndex = 1,Text = "2",UseVisualStyleBackColor = true};button8.Click += Button8_Click;// // button7// button7 = new Button{Location = new Point(6, 25),Name = "button7",Size = new Size(32, 23),TabIndex = 0,Text = "1",UseVisualStyleBackColor = true};button7.Click += Button7_Click;// // textBox1// textBox1 = new TextBox{Location = new Point(12, 12),Name = "textBox1",Size = new Size(215, 23),TabIndex = 0};// // groupBox1// groupBox1 = new GroupBox{Location = new Point(12, 41),Name = "groupBox1",Size = new Size(127, 148),TabIndex = 1,TabStop = false,Text = "数字"};groupBox1.Controls.Add(button16);groupBox1.Controls.Add(button14);groupBox1.Controls.Add(button15);groupBox1.Controls.Add(button11);groupBox1.Controls.Add(button12);groupBox1.Controls.Add(button13);groupBox1.Controls.Add(button10);groupBox1.Controls.Add(button9);groupBox1.Controls.Add(button8);groupBox1.Controls.Add(button7);groupBox1.SuspendLayout();// // button6// button6 = new Button{Location = new Point(6, 112),Name = "button6",Size = new Size(69, 23),TabIndex = 5,Text = "=",UseVisualStyleBackColor = true};button6.Click += Button6_Click;// // button5// button5 = new Button{Location = new Point(44, 83),Name = "button5",Size = new Size(32, 23),TabIndex = 4,Text = "/",UseVisualStyleBackColor = true};button5.Click += Button5_Click;// // button4// button4 = new Button{Location = new Point(7, 83),Name = "button4",Size = new Size(32, 23),TabIndex = 3,Text = "*",UseVisualStyleBackColor = true};button4.Click += Button4_Click;// // button3// button3 = new Button{Location = new Point(44, 54),Name = "button3",Size = new Size(32, 23),TabIndex = 2,Text = "-",UseVisualStyleBackColor = true};button3.Click += Button3_Click;// // button2// button2 = new Button{Location = new Point(7, 54),Name = "button2",Size = new Size(32, 23),TabIndex = 1,Text = "+",UseVisualStyleBackColor = true};button2.Click += Button2_Click;// // button1// button1 = new Button{Location = new Point(7, 25),Name = "button1",Size = new Size(69, 23),TabIndex = 0,Text = "C",UseVisualStyleBackColor = true};button1.Click += Button1_Click;// // groupBox2// groupBox2 = new GroupBox{Location = new Point(145, 41),Name = "groupBox2",Size = new Size(82, 148),TabIndex = 0,TabStop = false,Text = "运算符"};groupBox2.Controls.Add(button6);groupBox2.Controls.Add(button5);groupBox2.Controls.Add(button4);groupBox2.Controls.Add(button3);groupBox2.Controls.Add(button2);groupBox2.Controls.Add(button1);groupBox2.SuspendLayout();// // Form1// AutoScaleDimensions = new SizeF(7F, 17F);AutoScaleMode = AutoScaleMode.Font;ClientSize = new Size(239, 201);Controls.Add(groupBox2);Controls.Add(groupBox1);Controls.Add(textBox1);Name = "Form1";Text = "封装类实现计算器";groupBox1.ResumeLayout(false);groupBox2.ResumeLayout(false);}/// <summary>/// C/// </summary>private void Button1_Click(object? sender, EventArgs e){textBox1!.Text = "";//清空文本框}/// <summary>/// +/// </summary>private void Button2_Click(object? sender, EventArgs e){i = Convert.ToInt32(textBox1!.Text);//记录第一个数type = "+";//记录运算类型textBox1.Text = "";//清空文本框}/// <summary>/// -/// </summary>private void Button3_Click(object? sender, EventArgs e){i = Convert.ToInt32(textBox1!.Text);type = "-";textBox1.Text = "";}/// <summary>/// */// </summary>private void Button4_Click(object? sender, EventArgs e){i = Convert.ToInt32(textBox1!.Text);type = "*";textBox1.Text = "";}/// <summary>/// //// </summary>private void Button5_Click(object? sender, EventArgs e){i = Convert.ToInt32(textBox1!.Text);type = "/";textBox1.Text = "";}/// <summary>/// =/// </summary>private void Button6_Click(object? sender, EventArgs e){j = Convert.ToInt32(textBox1!.Text);//记录第二个数if (type == "/" && j == 0)//判断运算类型是不是除法{MessageBox.Show("被除数不能为0");}else{textBox1.Text = CCount.Sum(i, j, type).ToString();//计算结果}}/// <summary>/// 1/// </summary>private void Button7_Click(object? sender, EventArgs e){if (textBox1!.Text != "")//判断是否已经输入了数字textBox1.Text += "1";//如果已经输入,并且不是0,则加1elsetextBox1.Text = "1";}/// <summary>/// 2/// </summary>private void Button8_Click(object? sender, EventArgs e){if (textBox1!.Text != "")textBox1.Text += "2";elsetextBox1.Text = "2";}/// <summary>/// 3/// </summary>private void Button9_Click(object? sender, EventArgs e){if (textBox1!.Text != "")textBox1.Text += "3";elsetextBox1.Text = "3";}/// <summary>/// 4/// </summary>private void Button10_Click(object? sender, EventArgs e){if (textBox1!.Text != "")textBox1.Text += "4";elsetextBox1.Text = "4";}/// <summary>/// 5/// </summary>private void Button11_Click(object? sender, EventArgs e){if (textBox1!.Text != "")textBox1.Text += "5";elsetextBox1.Text = "5";}/// <summary>/// 6/// </summary>private void Button12_Click(object? sender, EventArgs e){if (textBox1!.Text != "")textBox1.Text += "6";elsetextBox1.Text = "6";}/// <summary>/// 7/// </summary>private void Button13_Click(object? sender, EventArgs e){if (textBox1!.Text != "")textBox1.Text += "7";elsetextBox1.Text = "7";}/// <summary>/// 8/// </summary>private void Button14_Click(object? sender, EventArgs e){if (textBox1!.Text != "")textBox1.Text += "8";elsetextBox1.Text = "8";}/// <summary>/// 9/// </summary>private void Button15_Click(object? sender, EventArgs e){if (textBox1!.Text != "")textBox1.Text += "9";elsetextBox1.Text = "9";}/// <summary>/// 0/// </summary>private void Button16_Click(object? sender, EventArgs e){if (textBox1!.Text != "")textBox1.Text += "0";elsetextBox1.Text = "0";}}/// <summary>/// 定义一个方法,用来计算两个数的和、差、积、商/// </summary>public class CCount{public static int Sum(int a, int b, string type){return type switch//判断运算符类型{"+" => a + b,"-" => a - b,"*" => a * b,"/" => a / b,_ => 0,};}}
}

2.生成效果

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/2780574.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

MySQL数据库-MVCC多版本并发控制

mvcc,多版本并发控制&#xff08;Multi-Version Concurrency Control&#xff09;,是一种用于数据库管理系统中的并发控制方法. 在传统的并发控制方法中,如锁定机制,当一个事务修改数据时,会对相关的数据对象进行锁定,其他事务需要等待该锁释放才能进行操作。这种方法存在着事…

Stable Diffusion 模型下载:RealCartoon-Anime - V10

本文收录于《AI绘画从入门到精通》专栏,专栏总目录:点这里。 文章目录 模型介绍生成案例案例一案例二案例三案例四案例五案例六案例七案例八案例九案例十

第5集《佛说四十二章经》

和尚尼慈悲、诸位法师、诸位居士&#xff0c;阿弥陀佛&#xff01; 请大家打开讲义第五面&#xff0c;第三章、割爱去贪。 蕅益大师他把《四十二章经》的内涵分成两个部分&#xff1a;第一部分是第一章、第二章的正道法门&#xff1b;其次&#xff0c;第三章之后共有四十章都…

Vue3中Setup概述和使用(三)

一、引入Setup 1、Person.Vue 与Vue3编写简单的App组件(二) 中的区别是&#xff1a;取消data、methods等方法,而是将数据和方法定义全部放进setup中。 <template><div class"person"><h1>姓名:{{name}}</h1><h1>年龄:{{age}}</h…

车载软件架构 —— Adaptive AUTOSAR是软件架构的正解吗?

车载软件架构 —— Adaptive AUTOSAR是软件架构的正解吗&#xff1f; 我是穿拖鞋的汉子&#xff0c;魔都中坚持长期主义的汽车电子工程师&#xff08;Wechat&#xff1a;gongkenan2013&#xff09;。 老规矩&#xff0c;分享一段喜欢的文字&#xff0c;避免自己成为高知识低文…

【JVM篇】分析并讲解字节码文件

文章目录 &#x1f354;字节码文件⭐打开字节码文件的工具⭐字节码文件的组成✨具体分析 &#x1f354;字节码文件 字节码文件是一种中间表示形式&#xff0c;它通常由编译器将高级编程语言&#xff08;如Java、Python等&#xff09;源代码编译而成。字节码文件包含了程序的指…

分享88个文字特效,总有一款适合您

分享88个文字特效&#xff0c;总有一款适合您 88个文字特效下载链接&#xff1a;https://pan.baidu.com/s/1Y0JCf4vLyxIJR6lfT9VHvg?pwd8888 提取码&#xff1a;8888 Python采集代码下载链接&#xff1a;采集代码.zip - 蓝奏云 学习知识费力气&#xff0c;收集整理更不…

Java串口通信技术探究1:深入理解RXTX库

目录 一、RXTX库简介二、RXTX库工作原理三、RXTX库的应用场景四、RXTX库的兼容性五、RXTX库下载及安装 在现代嵌入式系统中&#xff0c;串口通信是一种非常常见的通信方式。通过串口&#xff0c;设备之间可以传输各种类型和格式的信息。Java作为一种流行的编程语言&#xff0c;…

C++多线程学习[五]:RAII锁

一、什么是RAII 使用局部对象来控制资源的技术&#xff0c;即它的生命周期由操作系统来管理&#xff0c;无需人工的介入。 为什么要采用RAII技术呢&#xff1f; 主要是在开发过程中资源的销毁容易忘记&#xff0c;容易造成死锁或内存泄露。 {}为一个区域 &#xff0c;这里锁的…

java nio零拷贝

零拷贝是一种计算机执行IO操作的优化技术&#xff0c;其核心目标是减少数据拷贝次数&#xff0c;从而提高系统性能。它主要体现在以下几个方面&#xff1a; 1. **定义与原理**&#xff1a;零拷贝字面上的意思包括“零”和“拷贝”。其中&#xff0c;“拷贝”是指数据从一个存储…

单片机与外设的交互

单片机与外设的交互是嵌入式系统中非常重要的一个基础知识点。单片机是一个集成在同一芯片上的中央处理器、存储器和输入/输出接口,它可以根据用户编写的程序与各种外部设备即外设进行交互。单片机与外设之间的交互主要通过单片机上的输入/输出口(I/O口)来实现。 I/O口的工作原…

Python访问数据库

目录 SQLite数据库 SQLite数据类型 Python数据类型与SQLite数据类型的映射 使用GUI管理工具管理SQLite数据库 数据库编程的基本操作过程 sqlite3模块API 数据库连接对象Connection 游标对象Cursor 数据库的CRUD操作示例 示例中的数据表 无条件查询 有条件查询 插入…

快速学习Spring

Spring 简介 Spring 是一个开源的轻量级、非侵入式的 JavaEE 框架&#xff0c;它为企业级 Java 应用提供了全面的基础设施支持。Spring 的设计目标是简化企业应用的开发&#xff0c;并解决 Java 开发中常见的复杂性和低效率问题。 Spring常用依赖 <dependencies><!-…

【C语言】C的整理记录

前言 该笔记是建立在已经系统学习过C语言的基础上&#xff0c;笔者对C语言的知识和注意事项进行整理记录&#xff0c;便于后期查阅&#xff0c;反复琢磨。C语言是一种面向过程的编程语言。 原想在此阐述一下C语言的作用&#xff0c;然而发觉这些是编程语言所共通的作用&#…

使用securecrt+xming通过x11访问ubuntu可视化程序

windows使用securecrtxming通过x11访问ubuntu可视化程序 windows机器IP&#xff1a;192.168.9.133 ubuntu-desktop20.04机器IP&#xff1a;192.168.9.190 windows下载xming并安装 按照图修改xming配置 开始->xming->Xlaunch 完成xming会在右下角后台运行 windows在…

LeetCode Python -8.字符串转整数

文章目录 题目答案运行结果 题目 请你来实现一个 myAtoi(string s) 函数&#xff0c;使其能将字符串转换成一个 32 位有符号整数&#xff08;类似 C/C 中的 atoi 函数&#xff09;。 函数 myAtoi(string s) 的算法如下&#xff1a; 读入字符串并丢弃无用的前导空格检查下一个…

大数据Doris(六十五):基于Apache Doris的数据中台2.0

文章目录 基于Apache Doris的数据中台2.0 一、​​​​​​​架构升级

分享76个时间日期JS特效,总有一款适合您

分享76个时间日期JS特效&#xff0c;总有一款适合您 76个时间日期JS特效下载链接&#xff1a;https://pan.baidu.com/s/1s7tPGT_ItK7dNK5_qbZkug?pwd8888 提取码&#xff1a;8888 Python采集代码下载链接&#xff1a;采集代码.zip - 蓝奏云 学习知识费力气&#xff0c;…

C语言 服务器编程-日志系统

日志系统的实现 引言最简单的日志类 demo按天日志分类和超行日志分类日志信息分级同步和异步两种写入方式 引言 日志系统是通过文件来记录项目的 调试信息&#xff0c;运行状态&#xff0c;访问记录&#xff0c;产生的警告和错误的一个系统&#xff0c;是项目中非常重要的一部…