📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace 例5_1派生类
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.TextBox textBox6;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.textBox5 = new System.Windows.Forms.TextBox();
this.textBox6 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.label8 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(128, 51);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(96, 25);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(259, 51);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(96, 25);
this.textBox2.TabIndex = 1;
this.textBox2.Text = "";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(389, 51);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(96, 25);
this.textBox3.TabIndex = 2;
this.textBox3.Text = "";
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(520, 51);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(96, 25);
this.textBox4.TabIndex = 3;
this.textBox4.Text = "";
//
// textBox5
//
this.textBox5.Location = new System.Drawing.Point(256, 113);
this.textBox5.Name = "textBox5";
this.textBox5.Size = new System.Drawing.Size(96, 25);
this.textBox5.TabIndex = 4;
this.textBox5.Text = "";
//
// textBox6
//
this.textBox6.Location = new System.Drawing.Point(523, 113);
this.textBox6.Name = "textBox6";
this.textBox6.Size = new System.Drawing.Size(96, 25);
this.textBox6.TabIndex = 5;
this.textBox6.Text = "";
//
// label2
//
this.label2.Location = new System.Drawing.Point(149, 10);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(64, 31);
this.label2.TabIndex = 8;
this.label2.Text = "学号";
this.label2.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
//
// label3
//
this.label3.Location = new System.Drawing.Point(277, 10);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(64, 31);
this.label3.TabIndex = 9;
this.label3.Text = "姓名";
this.label3.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
//
// label4
//
this.label4.Location = new System.Drawing.Point(405, 10);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(64, 31);
this.label4.TabIndex = 10;
this.label4.Text = "性别";
this.label4.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
//
// label5
//
this.label5.Location = new System.Drawing.Point(533, 10);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(64, 31);
this.label5.TabIndex = 11;
this.label5.Text = "年龄";
this.label5.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
//
// label6
//
this.label6.Location = new System.Drawing.Point(181, 113);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(64, 31);
this.label6.TabIndex = 12;
this.label6.Text = "成绩1";
this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// label7
//
this.label7.Location = new System.Drawing.Point(437, 113);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(64, 31);
this.label7.TabIndex = 13;
this.label7.Text = "成绩2";
this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// label8
//
this.label8.Location = new System.Drawing.Point(43, 175);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(533, 92);
this.label8.TabIndex = 14;
//
// button1
//
this.button1.Location = new System.Drawing.Point(587, 175);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(85, 29);
this.button1.TabIndex = 15;
this.button1.Text = "创建对象";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(587, 226);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(85, 30);
this.button2.TabIndex = 16;
this.button2.Text = "退出";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
this.ClientSize = new System.Drawing.Size(693, 295);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label8);
this.Controls.Add(this.label7);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBox6);
this.Controls.Add(this.textBox5);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "创建派生类对象";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
string idd=textBox1.Text;
string namex=textBox2.Text;
string sexx=textBox3.Text;
byte agex=Convert.ToByte(textBox4.Text);
byte scorex1=Convert.ToByte(textBox5.Text);
byte scorex2=Convert.ToByte(textBox6.Text);
Student_1 subS=new Student_1(idd,namex,sexx,agex,scorex1,scorex2);
label8.Text=subS.Show( )+"总分:"+subS.Total( ).ToString( )
+" 平均分:"+subS.Average( ).ToString( );
}
private void button2_Click(object sender, System.EventArgs e)
{
Application.Exit( );
}
}
public class Student //基类定义
{
protected string id;
protected string name;
protected string sex;
protected byte age;
public Student( ){ }
public Student(string idd,string namex,string sexx,byte agex)
{
id=idd;
name=namex;
sex=sexx;
age=agex;
}
public string Show( )
{
string show="学号:"+id.ToString( )+" 姓名:"+name
+" 性别:"+sex+" 年龄:"+age.ToString( )+'\n';
return show;
}
}
public class Student_1: Student //派生类定义
{
private byte score1,score2;
public Student_1( ){ }
public Student_1(string idd,string namex,string sexx, byte agex ,
byte scorex1,byte scorex2):base(idd,namex,sexx,agex)
{
score1=scorex1;
score2=scorex2;
}
public uint Total( ) // 求总分
{
return (uint)score1+(uint)score2;
}
public float Average( )
{
return (float)(score1+score2)/2; //求平均分
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -