实验G08类的建立_第1页
实验G08类的建立_第2页
实验G08类的建立_第3页
实验G08类的建立_第4页
实验G08类的建立_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验G08: 类的建立一、实验名称和性质所属课程面向对象程序设计 实验名称类的建立实验学时2实验性质验证 综合 设计必做/选做必做 选做二、实验目的1理解C#语言是如何体现面向对象编程基本思想;2掌握类对象的定义;3了解类的封装方法,以及如何创建类和对象;了解成员变量和成员方法的特性;4掌握构造函数和析构函数的含义与作用、定义方式和实现,能够根据要求正确定义和重载构造函数。能够根据给定的要求定义类并实现类的成员函数;5理解类的成员的访问控制的含义,公有、私有和保护成员的区别;三、实验的软硬件环境要求硬件环境要求:PC机(单机)使用的软件名称、版本号以及模块:Windows XP下的Visual

2、 Studio.NET2003或Visual Studio.NET2008四、知识准备前期要求掌握的知识:五、实验内容1编写一个包含类和类方法的程序;2编写一个创建对象和使用对象的方法程序;3编写含有构造方法的类的程序;4编写含有属性的类程序。六、验证性实验验证性实验11 实验要求定义一个圆类,计算圆的面积和周长2 实验步骤步骤1 在项目中public class circle public static void Main() double radium, delimeter, square; const double pai = 3.1415926; radium = Convert.To

3、Int32(Console.ReadLine(); delimeter = 2 * pai * radium; square = pai * pai * radium; Console.WriteLine("delimeter=0,square=1", delimeter, square); Console.ReadLine(); 或者:public class circle double delimeter, square; const double pai = 3.1415926; public void calculate(double rad) delimeter

4、= 2 * pai * rad; square = pai * pai * rad; Console.WriteLine("delimeter=0,square=1",delimeter,square); public static void Main() double radium; circle cir = new circle(); radium = Convert.ToInt32(Console.ReadLine(); cir.calculate(radium); Console.ReadLine(); 3思考题请比较以上两个程序,看起来后一个程序把问题复杂化了,是

5、不是不如第一个程序好,它从设计思想上有什么优势么?验证性实验21实验要求其中有3个数据成员有学号、姓名、年龄,以及若干成员函数。同时编写主函数使用这个类,实现对学生数据的赋值和输出。要求:使用成员函数实现对数据的输出;使用构造函数实现对数据的输入。2实验步骤步骤1 先建立类Person,点击“项目”菜单下的“添加类”,在如下的添加类窗口中输入类名Person后,按“确定”按钮。步骤2 进入类窗口编辑窗口,见下图。using System;namespace 试验3/ <summary>/ Class1 的摘要说明。/ </summary>public class Per

6、sonpublic Person()/ TODO: 在此处添加构造函数逻辑/步骤3 在类窗口中输入下面的代码; public class Personstring m_id,m_name;int m_age;public Person(string id,string name,int age)this.m_id = id;this.m_name = name;this.m_age = age;public string IDgetreturn m_id;setm_id=value;public string Namegetreturn m_name;setm_name=value;publi

7、c int Agegetreturn m_age;setm_age=value;public string Display()return "ID:"+m_id+" Name:"+m_name+" Age:"+m_age.ToString ("N0");步骤4 建立如下的窗口界面:3个标签、3个文本框、1个命令按钮和1个列表框。步骤5各标签控件按照上图的提示修改Text属性即可,文本框的Text属性设置为空。按照如下表设置其它控件的属性。textBox1NametxtIDtextBox1NametxtNametex

8、tBox1NametxtAgebutton1NamebtnAddTextAdd a personlistBox1NamelstDisplay步骤 双击窗体的“Add a person”命令按钮,编写的btnAdd_Click事件代码。private void btnAdd_Click(object sender, System.EventArgs e)Person person=new Person (txtID.Text,txtName.Text,int.Parse(txtAge.Text);lstDisplay.Items.Add(person.Display ();步骤7 运行程序,在文

9、本框中输入人的ID、Name和Age后,点击btnAdd按钮,得到上面图中的结果。3思考题以上程序使用了构造方法,请问在窗体中实例化一个Person后,能访问m_name变量吗?哪些能访问?验证性实验3 实现一个日期类型1实验要求定义日期类型Date。要求有以下面成员:年、月、日变量,重载的构造方法,一个实现年、月、日单独的输入,一个实现从系统时间里读出年月日,并实现打印方法成员,该方法要按照“XXXX年XX月XX日”格式输出日期。2源程序代码public class Date private int Year, Month, Day; public Date(int Year, int Mo

10、nth,int Day) this.Year=Year; this.Month=Month; this.Day=Day; public Date(System.DateTime dt) Year = dt.Year; Month = dt.Month; Day = dt.Day; public void DisplayDate() Console.WriteLine("0年1月2日",Year,Month,Day); public class Tester public static void Main() System.DateTime currentTime=Syste

11、m.DateTime.Now; Date dt=new Date(2008,7,18); dt.DisplayDate(); Date dt2 = new Date(currentTime); dt2.DisplayDate(); Console.ReadLine(); 3思考题请练习System.Datetime类型及其各属性的应用。列出System.Datetime主要包含哪些属性,并思考重载构造方法的作用。七、设计性实验1要求(1) 创建sandwitch 类。每个sandwitch对象包含属性 Name、 Bread、 Meat、Cheese及调味品。用户在窗体的文本框中输入相应属性的

12、值,并显示在另一个窗体上。程序代码: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 类一 public partial class Form1 : Form public Form1() InitializeComponent(); private void btnAdd_C

13、lick(object sender, EventArgs e) sandwitch sandwitch = new sandwitch(txtName.Text, txtDressing.Text, int.Parse(txtBread.Text),int.Parse(txtMeat.Text),int.Parse(txtCheese.Text); lisReslute.Items.Add(sandwitch.Display(); Sandwitch的类using System;using System.Collections.Generic;using System.Linq;using

14、System.Text;namespace 类一 class sandwitch string m_name, m_dressing; int m_bread, m_meat, m_cheese; public sandwitch(string name, string dressing, int bread, int meat, int cheese) this.m_name = name; this.m_dressing = dressing; this.m_bread = bread; this.m_meat = meat; this.m_cheese = cheese; public

15、string Name get return m_name; set m_name = value; public string Dressing get return m_dressing; set m_dressing = value; public int Bread get return m_bread; set m_bread = value; public int Meat get return m_meat; set m_meat = value; public int Cheese get return m_cheese; set m_cheese = value; publi

16、c string Display() return "Name:" + m_name + " Dressing:" + m_dressing + " Bread:" + m_bread.ToString() + " Meat:" + m_meat.ToString() + " Cheese:" + m_cheese.ToString(); (2) 修改实验5中Cool Boards 项目,将用户信息从逻辑设计中分离。即创建一个类theShirt,包含属性Order Number、Quantity

17、、Size、 Monogram 和Pocket,并定义方法Price来计算该衬衫的价格。程序代码: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 类形式计算衬衫价格 public partial class Form1 : Form public Form1() Initia

18、lizeComponent(); private void btnAdd_Click(object sender, EventArgs e) theShirt shirt = new theShirt(txtName.Text, int.Parse(txtOrder.Text), int.Parse(txtNum.Text), chkMonogram.Checked, chkPocket.Checked, rbtnSmall.Checked, rbtnMedium.Checked, rbtnLarge.Checked, rbtnExtraLarge.Checked, rbtnXXL.Check

19、ed); lisReslute.Items.Add(shirt.Display(); lisReslute.Items.Add(shirt.Price(); private void btnClear_Click(object sender, EventArgs e) txtName.Text = "" txtOrder.Text = "" txtNum.Text = "" lisReslute.Text = "" rbtnMedium.Checked = false; rbtnSmall.Checked = fa

20、lse; rbtnLarge.Checked = false; rbtnExtraLarge.Checked = false; rbtnXXL.Checked = false; chkMonogram.Checked = false; chkPocket.Checked = false; 另外的一个类:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 类形式计算衬衫价格 public class theShirt int m_OrderNumber, m_Qua

21、ntity; double total1, total2; Boolean m_monogram, m_pocket,m_small,m_medium,m_large,m_exlarge,m_xxl; double Pmonogram, Ppocket, Psize; string m_Name; public theShirt(string name, int OrderNumber, int Quantity, bool monogram, bool pocket, Boolean small, Boolean medium, Boolean large, Boolean exlarge,

22、 Boolean xxl) this.m_Name = name; this.m_OrderNumber = OrderNumber; this.m_Quantity = Quantity; this.m_monogram = monogram; this.m_pocket = pocket; this.m_small = small; this.m_medium = medium; this.m_large = large; this.m_exlarge = exlarge; this.m_xxl = xxl; public string Name get return m_Name; se

23、t m_Name = value; public int OrderNumber get return m_OrderNumber; set m_OrderNumber = value; public int Quantity get return m_Quantity; set m_Quantity = value; public Boolean Monogram get return m_monogram; set m_monogram = value; public Boolean Pocket get return m_pocket; set m_pocket = value; public Boolean Small get return m_small; set m_small = value; public Boolean Medium get return m_medium; set m_medium = value; public Boolean Large get return m_large; set m_large = value; public Boolean Exlarge get return m_exlarg

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论