《实验报告四(共9页).doc》由会员分享,可在线阅读,更多相关《实验报告四(共9页).doc(9页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上攀 枝 花 学 院 实 验 报 告实验课程:Visual C#.NET程序设计教程 实验项目:上机实验6 实验日期:2015.05.12系:数计学院 成绩: 一、 实验目的1、 初步掌握常用集合的创建和操作方法。2、 初步掌握索引器的定义和使用。3、 初步掌握泛型接口、泛型类、泛型属性和泛型方法的使用。二、 实验要求1. 熟悉Visual Studio.Net2010的基本操作方法。2. 认真阅读本章相关内容,尤其是案例。3. 实验前进行程序设计,完成源程序的编写任务。4. 反复操作,直到不需要参考教材、能熟练操作为止。 三、 实验步骤1、 设计一个Windows应用
2、程序,定义一个Teacher类,包含姓名和职称两个字段和一个输出自己信息的方法,并用ArrayList实现与实例6_1相同的功能。源程序如下: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;using System.Collections;namespace Test4_1 public par
3、tial class Form1 : Form public Form1() InitializeComponent(); public class Teacher private string name; private string job; public Teacher(string name, string job) this.name = name; this.job = job; public string GetMessage() return string.Format(姓名:0n职称:1。, this.name, this.job); ArrayList a = new Ar
4、rayList(); private void Show() foreach (object btObj in a) Teacher btErgodic = (Teacher)btObj; lblShow.Text += n + btErgodic.GetMessage(); private void btAdd_Click(object sender, EventArgs e) Teacher x = new Teacher(txtName.Text, txtJob.Text); a.Add(x); lblShow.Text = ; Show(); private void btInster
5、_Click(object sender, EventArgs e) int Index = Convert.ToInt32(txtIndex.Text); Teacher x = new Teacher(txtName.Text,txtJob.Text); a.Insert(Index, x); lblShow.Text = ; Show(); private void btDelete_Click(object sender, EventArgs e) int Index = Convert.ToInt32(txtIndex.Text); a.RemoveAt(Index); lblSho
6、w.Text = ; Show(); private void btErgodic_Click(object sender, EventArgs e) lblShow.Text = ; Show(); 运行结果如图所示:2、 设计一个Windows应用程序,定义一个Student类,包含学号和姓名两个字段,并定义一个班级类ClassList,该类包括一个Student集合,使用索引器访问该集合,实现与实例6_3类似的功能。源程序如下:using System;using System.Collections.Generic;using System.ComponentModel;using S
7、ystem.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace Test4_2 public partial class Form1 : Form public Form1() InitializeComponent(); public class Student private string number; private string name; public Student(string number,string name) this.num
8、ber = number; this.name = name; public string Number get return number; public string Name get return name; public class ClassList private Student student; public ClassList(int x) student = new Studentx; public Student thisint index get if (index = student.Length) return null; return studentindex; s
9、et if (index = student.Length) return; studentindex = value; public Student thisstring get get foreach (Student s in student) if (s.Name.IndexOf(get) != -1 | s.Number.IndexOf(get) != -1) return s; return null; ClassList a = new ClassList(50); private void btAdd_Click(object sender, EventArgs e) int
10、index = Convert.ToInt32(txtFigure.Text) - 1; Student s = new Student(txtNumber.Text, txtName.Text); aindex=s; lblShow.Text = string.Format(第0位同学信息添加成功!n 学号:1!姓名:2, index + 1,s.Number,s.Name); private void btNumber_Click(object sender, EventArgs e) Student s = atxtNumber.Text; if (s != null) lblShow.
11、Text = string.Format(找到学号为:0的同学的信息!n学号:1!姓名:2, txtNumber.Text, s.Number, s.Name); else lblShow.Text = string.Format(没有找到学号为:0的同学的信息,txtNumber.Text); private void btName_Click(object sender, EventArgs e) Student s = atxtName.Text; if (s != null) lblShow.Text = string.Format(找到姓名为:0的同学的信息!n学号:1!姓名:2,
12、txtName.Text, s.Number, s.Name); else lblShow.Text = string.Format(没有找到姓名为:0的同学的信息,txtName.Text); private void btShow_Click(object sender, EventArgs e) int index = Convert.ToInt32(txtFigure.Text)-1; Student s = aindex; if (s != null) lblShow.Text = string.Format(第0位同学的信息为:n学号:1姓名:2, index + 1, s.Num
13、ber, s.Name); else lblShow.Text = string.Format(没有第0位同学的信息!,index+1); 运行结果如图所示:3、 设计一个Windows应用程序,要求如下。(1) 构造一个产品基本类。(2) 分别定义家电、日用百货、衣服等派生类,要求具有不同的特征和行为。(3) 定义一个泛型货架类,约束参数类型为产品类,该泛型的货架类包括一个泛型集合,用于存放各种产品对象,并包含一个方法,用于输出每个产品的相关信息。(4) 再仿照实例6_4,定义泛型的货架类对象,完成对产品的添加和信息的输出。源程序如下:using System;using System.Co
14、llections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace Test4_3 public partial class Form1 : Form public Form1() InitializeComponent(); Product sh = new Product(); private void btAdd1_Click(object s
15、ender, EventArgs e) sh.shangpings.Add(new Jiadian(txtName.Text); lblShow.Text += string.Format(n添加家电类:0成功, txtName.Text); private void btAdd2_Click(object sender, EventArgs e) sh.shangpings.Add(new Bauhuo(txtName.Text); lblShow.Text += string.Format(n添加日用百货类:0成功, txtName.Text); private void btAdd3_C
16、lick(object sender, EventArgs e) sh.shangpings.Add(new Yifu(txtName.Text); lblShow.Text += string.Format(n添加衣服类:0成功, txtName.Text); private void btPut_Click(object sender, EventArgs e) lblShow.Text = sh.ShangjiaTo(); public abstract class Shangpin public string name; public Shangpin(string name) thi
17、s.name = name; public abstract string Shangjia(); public class Jiadian : Shangpin public Jiadian(string name) : base(name) public override string Shangjia() return string.Format(0:家电类:请轻拿轻放!, name); public class Bauhuo : Shangpin public Bauhuo(string name) : base(name) public override string Shangji
18、a() return string.Format(0:日用百货类:请仔细清点数目!, name); public class Yifu : Shangpin public Yifu(string name) : base(name) public override string Shangjia() return string.Format(0:衣服类:材质易损,请托管保留!, name); public class Product where T : Shangpin private List shs = new List(); public List shangpings get return shs; public string ShangjiaTo() string msg = string.Empty; foreach (T sh in shs) msg += n + sh.Shangjia(); return msg; 运行结果如图所示:四、实验总结通过这次实验,了解了常用集合的创建和操作方法,索引器的定义和使用,泛型接口、泛型类、泛型属性和泛型方法的使用,这次实验中,对于泛型的使用并不是十分熟练,还需要多加练习,实验过程中,对于理论知识有了更深层次的理解,不再那么片面。专心-专注-专业