目录
一、涉及到的知识点
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.生成效果