整理教材示例:项目 1 任务 2、项目 4 任务 1 和 3、项目 5 任务 1
- 界面取自配套参考工程,用设计器引擎按书上表格改控件名(label1 → lbl_port 等), 去掉 BOM、换行统一为 LF;代码按书整理,修正 boolIsOpen 等印刷错误 - 测试:所有示例都能编译(10 个),以及计算器 / 多态、红外对射、报警灯、火焰的行为测试 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8feda289de
commit
4c53caf7fa
22 files changed
+1101
-7
No files matched your search
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace C_基础
|
||||
{
|
||||
class American : People//继承People类
|
||||
{
|
||||
public override void Speak(System.Windows.Forms.TextBox Txtbox, string name)//重写父类中的虚方法
|
||||
{
|
||||
base.Speak(Txtbox, name+"说的是英语!\r\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace C_基础
|
||||
{
|
||||
class Chinese:People//继承People类
|
||||
{
|
||||
public override void Speak(System.Windows.Forms.TextBox Txtbox, string name)//重写父类中的虚方法
|
||||
{
|
||||
base.Speak(Txtbox, name+"说的是汉语!\r\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
Generated
+162
@@ -0,0 +1,162 @@
|
||||
namespace C_基础
|
||||
{
|
||||
partial class Form1
|
||||
{
|
||||
/// <summary>
|
||||
/// 必需的设计器变量。
|
||||
/// </summary>
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
/// <summary>
|
||||
/// 清理所有正在使用的资源。
|
||||
/// </summary>
|
||||
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
#region Windows 窗体设计器生成的代码
|
||||
|
||||
/// <summary>
|
||||
/// 设计器支持所需的方法 - 不要
|
||||
/// 使用代码编辑器修改此方法的内容。
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.groupBox1 = new System.Windows.Forms.GroupBox();
|
||||
this.btnCalculate = new System.Windows.Forms.Button();
|
||||
this.txtResult = new System.Windows.Forms.TextBox();
|
||||
this.cmboxType = new System.Windows.Forms.ComboBox();
|
||||
this.txtNum2 = new System.Windows.Forms.TextBox();
|
||||
this.txtNum1 = new System.Windows.Forms.TextBox();
|
||||
this.groupBox2 = new System.Windows.Forms.GroupBox();
|
||||
this.txtDispaly = new System.Windows.Forms.TextBox();
|
||||
this.btnDisplay = new System.Windows.Forms.Button();
|
||||
this.groupBox1.SuspendLayout();
|
||||
this.groupBox2.SuspendLayout();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// groupBox1
|
||||
//
|
||||
this.groupBox1.Controls.Add(this.btnCalculate);
|
||||
this.groupBox1.Controls.Add(this.txtResult);
|
||||
this.groupBox1.Controls.Add(this.cmboxType);
|
||||
this.groupBox1.Controls.Add(this.txtNum2);
|
||||
this.groupBox1.Controls.Add(this.txtNum1);
|
||||
this.groupBox1.Location = new System.Drawing.Point(21, 12);
|
||||
this.groupBox1.Name = "groupBox1";
|
||||
this.groupBox1.Size = new System.Drawing.Size(516, 80);
|
||||
this.groupBox1.TabIndex = 0;
|
||||
this.groupBox1.TabStop = false;
|
||||
this.groupBox1.Text = "简易计算器";
|
||||
//
|
||||
// btnCalculate
|
||||
//
|
||||
this.btnCalculate.Location = new System.Drawing.Point(325, 31);
|
||||
this.btnCalculate.Name = "btnCalculate";
|
||||
this.btnCalculate.Size = new System.Drawing.Size(49, 23);
|
||||
this.btnCalculate.TabIndex = 5;
|
||||
this.btnCalculate.Text = "=";
|
||||
this.btnCalculate.UseVisualStyleBackColor = true;
|
||||
this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click);
|
||||
//
|
||||
// txtResult
|
||||
//
|
||||
this.txtResult.Location = new System.Drawing.Point(390, 31);
|
||||
this.txtResult.Name = "txtResult";
|
||||
this.txtResult.ReadOnly = true;
|
||||
this.txtResult.Size = new System.Drawing.Size(100, 21);
|
||||
this.txtResult.TabIndex = 4;
|
||||
//
|
||||
// cmboxType
|
||||
//
|
||||
this.cmboxType.FormattingEnabled = true;
|
||||
this.cmboxType.Items.AddRange(new object[] {
|
||||
"+",
|
||||
"-",
|
||||
"*",
|
||||
"/"});
|
||||
this.cmboxType.Location = new System.Drawing.Point(112, 31);
|
||||
this.cmboxType.Name = "cmboxType";
|
||||
this.cmboxType.Size = new System.Drawing.Size(64, 20);
|
||||
this.cmboxType.TabIndex = 2;
|
||||
//
|
||||
// txtNum2
|
||||
//
|
||||
this.txtNum2.Location = new System.Drawing.Point(195, 31);
|
||||
this.txtNum2.Name = "txtNum2";
|
||||
this.txtNum2.Size = new System.Drawing.Size(100, 21);
|
||||
this.txtNum2.TabIndex = 1;
|
||||
//
|
||||
// txtNum1
|
||||
//
|
||||
this.txtNum1.Location = new System.Drawing.Point(6, 31);
|
||||
this.txtNum1.Name = "txtNum1";
|
||||
this.txtNum1.Size = new System.Drawing.Size(100, 21);
|
||||
this.txtNum1.TabIndex = 0;
|
||||
//
|
||||
// groupBox2
|
||||
//
|
||||
this.groupBox2.Controls.Add(this.txtDispaly);
|
||||
this.groupBox2.Controls.Add(this.btnDisplay);
|
||||
this.groupBox2.Location = new System.Drawing.Point(21, 98);
|
||||
this.groupBox2.Name = "groupBox2";
|
||||
this.groupBox2.Size = new System.Drawing.Size(516, 116);
|
||||
this.groupBox2.TabIndex = 1;
|
||||
this.groupBox2.TabStop = false;
|
||||
this.groupBox2.Text = "类的多态性";
|
||||
//
|
||||
// txtDispaly
|
||||
//
|
||||
this.txtDispaly.Location = new System.Drawing.Point(46, 20);
|
||||
this.txtDispaly.Multiline = true;
|
||||
this.txtDispaly.Name = "txtDispaly";
|
||||
this.txtDispaly.Size = new System.Drawing.Size(249, 82);
|
||||
this.txtDispaly.TabIndex = 1;
|
||||
//
|
||||
// btnDisplay
|
||||
//
|
||||
this.btnDisplay.Location = new System.Drawing.Point(344, 45);
|
||||
this.btnDisplay.Name = "btnDisplay";
|
||||
this.btnDisplay.Size = new System.Drawing.Size(75, 23);
|
||||
this.btnDisplay.TabIndex = 0;
|
||||
this.btnDisplay.Text = "显示";
|
||||
this.btnDisplay.UseVisualStyleBackColor = true;
|
||||
this.btnDisplay.Click += new System.EventHandler(this.btnDisplay_Click);
|
||||
//
|
||||
// Form1
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(549, 226);
|
||||
this.Controls.Add(this.groupBox2);
|
||||
this.Controls.Add(this.groupBox1);
|
||||
this.Name = "Form1";
|
||||
this.Text = "C#入门基础";
|
||||
this.groupBox1.ResumeLayout(false);
|
||||
this.groupBox1.PerformLayout();
|
||||
this.groupBox2.ResumeLayout(false);
|
||||
this.groupBox2.PerformLayout();
|
||||
this.ResumeLayout(false);
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private System.Windows.Forms.GroupBox groupBox1;
|
||||
private System.Windows.Forms.TextBox txtResult;
|
||||
private System.Windows.Forms.ComboBox cmboxType;
|
||||
private System.Windows.Forms.TextBox txtNum2;
|
||||
private System.Windows.Forms.TextBox txtNum1;
|
||||
private System.Windows.Forms.Button btnCalculate;
|
||||
private System.Windows.Forms.GroupBox groupBox2;
|
||||
private System.Windows.Forms.TextBox txtDispaly;
|
||||
private System.Windows.Forms.Button btnDisplay;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace C_基础
|
||||
{
|
||||
public partial class Form1 : Form
|
||||
{
|
||||
public Form1()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void btnCalculate_Click(object sender, EventArgs e)
|
||||
{
|
||||
if(txtNum1.Text.Trim()!=""&&txtNum2.Text.Trim()!="")
|
||||
{
|
||||
float a, b;
|
||||
a = float.Parse(txtNum1.Text);//字符串转化为float
|
||||
b = float.Parse(txtNum2.Text);//字符串转化为float
|
||||
float res = (float)Result(a, b, cmboxType.Text);//两数进行装箱,调用计算函数,返回值进行拆箱
|
||||
txtResult.Text = res.ToString();//将返回值显示到界面上
|
||||
}
|
||||
}
|
||||
public Object Result(Object a,Object b,String type)//计算结果,使用拆箱装箱方式
|
||||
{
|
||||
float result=0;
|
||||
float a1,b1;
|
||||
a1=(float)a;//拆箱
|
||||
b1=(float)b;//拆箱
|
||||
switch(type)
|
||||
{
|
||||
case "+": result = a1 + b1; break;//计算两个数的和
|
||||
case "-": result = a1 - b1; break;//计算两个数的差
|
||||
case "*": result = a1 * b1; break;//计算两个数的积
|
||||
case "/": result = a1 / b1; break;//计算两个数的商
|
||||
default: result=0;break;//默认值
|
||||
}
|
||||
return (Object)result;//装箱
|
||||
}
|
||||
|
||||
private void btnDisplay_Click(object sender, EventArgs e)
|
||||
{
|
||||
People pleTemp;//定义People临时存储变量
|
||||
Dictionary<int, string> dicName = new Dictionary<int, string>();//定义Dictionary字典类型,用于存储姓名与序号
|
||||
dicName.Add(1, "张三");//向字典中添加数据
|
||||
dicName.Add(2, "李四");
|
||||
dicName.Add(3, "Mary");
|
||||
dicName.Add(4, "Jimmy");
|
||||
List<People> peoplelist = new List<People>();//定义People的List泛型数据
|
||||
for (int i = 0; i < 2; i++)//添加两个中国人
|
||||
{
|
||||
pleTemp = new Chinese();//实例化中国人
|
||||
peoplelist.Add(pleTemp);
|
||||
}
|
||||
for (int i = 0; i < 2; i++)//添加两个美国人
|
||||
{
|
||||
pleTemp = new American();//实例化美国人
|
||||
peoplelist.Add(pleTemp);
|
||||
}
|
||||
for (int i = 0; i < peoplelist.Count; i++)//遍历List
|
||||
{
|
||||
peoplelist[i].Speak(this.txtDispaly, dicName[i + 1]);//执行派生类重写的方法
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace C_基础
|
||||
{
|
||||
class People//定义一个父类People
|
||||
{
|
||||
public virtual void Speak(TextBox Txtbox,string name)//定义一个虚方法,用来表示人类的说话语言
|
||||
{
|
||||
Txtbox.Text += name;//向控件中输出名字
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace C_基础
|
||||
{
|
||||
static class Program
|
||||
{
|
||||
/// <summary>
|
||||
/// 应用程序的主入口点。
|
||||
/// </summary>
|
||||
[STAThread]
|
||||
static void Main()
|
||||
{
|
||||
Application.EnableVisualStyles();
|
||||
Application.SetCompatibleTextRenderingDefault(false);
|
||||
Application.Run(new Form1());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"title": "项目1 任务2 C#面向对象特性(计算器、多态)",
|
||||
"source": "教材书 p13–16,界面取自配套参考工程,控件名和窗体标题按书修改(BtnCalculate → btnCalculate)"
|
||||
}
|
||||
Reference in new issue
Block a user