1. 設計一個Windows應用程序,在該程序中首先構造一個學生基本類,在分別構造小學生、中學生、中職生、大學生等派生類,要求具有不同的特征和行為,能通過靜態成員自動記錄不同的學生人數。
參考界面如下:
(1)修改一些控件的屬性。
(2)核心代碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class Student
{
private String Name;
private String Sex;
private StringSchool;
private String Grade;
public String SName
{
get { return Name; }
set { Name = value; }
}
public String SSex
{
get { return Sex; }
set { Sex = value; }
}
public String SSchool
{
get { return School; }
set { School = value; }
}
public String SGread
{
get { return Grade; }
set { Grade = value; }
}
}
public class A : Student
{
}
public class B : Student
{
}
public class C : Student
{
}
public class D : Student
{
}
public List<A> a = new List<A>();
public List<B> b = new List<B>();
public List<C> c = new List<C>();
public List<D> d = new List<D>();
private void button2_Click(object sender, EventArgs e)
{
A p = new A();
p.SName = textBox1.Text;
p.SSex = textBox2.Text;
p.SSchool = textBox3.Text;
p.SGread = textBox4.Text;
a.Add(p);
textBox4.Text = "";
textBox3.Text = "";
textBox2.Text = "";
textBox1.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
foreach (A p in a)
{
if (p.SName.Equals(textBox1.Text))
{
label5.Text = "姓?名?:êo" + p.SName + "\n"+"性?別Àe:êo" + p.SSex +"\n"+ "學¡ì校¡ê:êo" + p.SSchool+"\n" + "成¨¦績¡§:êo" + p.SGread;
}
else
{
label5.Text = "對?不?起e,ê?沒?有®D此ä?人¨?";
}
}
}
private void label5_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
B p = new B();
p.SName = textBox5.Text;
p.SSex = textBox6.Text;
p.SSchool = textBox7.Text;
p.SGread = textBox8.Text;
b.Add(p);
textBox5.Text = "";
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
}
private void button4_Click(object sender, EventArgs e)
{
foreach (B p in b)
{
if (p.SName.Equals(textBox8.Text))
{
label6.Text = "姓?名?:êo" + p.SName + "性?別Àe:êo" + p.SSex + "學¡ì校¡ê:êo" + p.SSchool + "成¨¦績¡§:êo" + p.SGread;
}
else
{
label6.Text = "對?不?起e,ê?沒?有®D此ä?人¨?";
}
}
}
private void button5_Click(object sender, EventArgs e)
{
C p = new C();
p.SName = textBox12.Text;
p.SSex = textBox11.Text;
p.SSchool = textBox10.Text;
p.SGread = textBox9.Text;
c.Add(p);
textBox12.Text = "";
textBox11.Text = "";
textBox10.Text = "";
textBox9.Text = "";
}
private void textBox12_TextChanged(object sender, EventArgs e)
{
}
private void textBox11_TextChanged(object sender, EventArgs e)
{
}
private void textBox10_TextChanged(object sender, EventArgs e)
{
}
private void textBox9_TextChanged(object sender, EventArgs e)
{
}
private void button6_Click(object sender, EventArgs e)
{
foreach (C p in c)
{
if (p.SName.Equals(textBox12.Text))
{
label11.Text = "姓?名?:êo" + p.SName + "性?別Àe:êo" + p.SSex + "學¡ì校¡ê:êo" + p.SSchool + "成¨¦績¡§:êo" + p.SGread;
}
else
{
label11.Text = "對?不?起e,ê?沒?有®D此ä?人¨?";
}
}
}
private void label11_Click(object sender, EventArgs e)
{
}
private void button7_Click(object sender, EventArgs e)
{
D p = new D();
p.SName = textBox16.Text;
p.SSex = textBox15.Text;
p.SSchool = textBox14.Text;
p.SGread = textBox13.Text;
d.Add(p);
textBox16.Text = "";
textBox15.Text = "";
textBox14.Text = "";
textBox13.Text = "";
}
private void textBox16_TextChanged(object sender, EventArgs e)
{
}
private void textBox15_TextChanged(object sender, EventArgs e)
{
}
private void textBox14_TextChanged(object sender, EventArgs e)
{
}
private void textBox13_TextChanged(object sender, EventArgs e)
{
}
private void button8_Click(object sender, EventArgs e)
{
foreach (D p in d)
{
if (p.SName.Equals(textBox16.Text))
{
label16.Text = "姓?名?:êo" + p.SName + "性?別Àe:êo" + p.SSex + "學¡ì校¡ê:êo" + p.SSchool + "成¨¦績¡§:êo" + p.SGread;
}
else
{
label16.Text = "對?不?起e,ê?沒?有®D此ä?人¨?";
}
}
}
private void label16_Click(object sender, EventArgs e)
{
}
}
}