C#实验7 面向对象程序设计.doc_第1页
C#实验7 面向对象程序设计.doc_第2页
C#实验7 面向对象程序设计.doc_第3页
C#实验7 面向对象程序设计.doc_第4页
C#实验7 面向对象程序设计.doc_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

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

文档简介

实验7 面向对象程序设计基础一 实验题目:面次对象程序设计基础二 目的和要求1. 掌握类和对象的使用2. 掌握类的继承3. 掌握构造函数和析构函数的使用4. 掌握(静态)方法,属性的使用5. 掌握方法的各种参数类型使用方法。三 实验内容 (注:本实验所建程序全部都是控制台程序)1 定义一个学生类Student,并实验Private ,public等修饰符的功能,实验对象的实例化过程。using System;namespace Example_PublicAndPrivate /定义一个学生类 public class Student /属性 public string strName;/公有属性 private int nAge;/私有属性/方法. public void SetAge(int _nAge) this.nAge = _nAge; / Main函数类class Test / 应用程序的主入口点。 static void Main(string args) Console.writeLine(“这里输出你的学号”); Student s = new Student(); s.strName = 张三“; /正确与否,原因 s.nAge = 20; /正确与否?原因s.SetAge(20); /赋值年龄 Console.WriteLine(s.GetAge();/获取年龄 2 类的继承。在1中实现的学生类的基础上,使用继承机制,设计一个大学生类,要求大学生类拥有年龄,姓名和系别属性。using System;namespace Example_Inheritance/ 学生类public class Student public string strName;/姓名 public int nAge;/年龄 / 大学生类:继承学生类 public class CollegeStudent : Student public string strInsititute;/所在系 public class MainClass / 主函数 static void Main(string args) Console.writeLine(“这里输出你的学号”); Student s = new Student(); s.strName = xiaobao; s.nAge=18; Console.WriteLine(姓名:0,年龄1, s.strName, s.nAge);/使用子类 Console.WriteLine(-使用子类-); CollegeStudent c = new CollegeStudent(); c.strName = 小宝; c.nAge=23; c.strInsititute = 电子系; Console.WriteLine(姓名:0,年龄:1岁,所属系:2, c.strName, c.nAge, c.strInsititute); Console.Read(); 3 类的构造函数和析构函数。实现Time类的构造函数及其重载。Time类具有三个属性:小时(nHour),分钟(nMinute),秒(nSecond)。分别实现构造函数的4中重载形式:不带参数,带一个参数,带两个参数,带三个参数。实现一个析构函数,在析构函数中输出一行文字:“Time() is called.”.class Time public int nHour, nMinute, nSecond; public Time() nHour = nMinute = nSecond = 0; public Time(int Hour) nHour = Hour; nMinute = nSecond = 0; public Time(int Hour,int Minute) nHour = Hour; nMinute = Minute; nSecond = 0; public Time(int Hour,int Minute,int Second) nHour = Hour; nMinute = Minute; nSecond = Second; Public Time() Console.WriteLine(“Time () is called “); class Test static void Main() Console.writeLine(“这里输出你的学号”); Time time1, time2, time3, time4;/对time1,time2,time3, time4分别调用不同的构造函数 time1 = new Time(); time2 = new Time(10); time3 = new Time(10, 30); time4 = new Time(10, 30, 30); Console.WriteLine(time 1的时间为:0时1分钟2秒, time 1.nHour, time 1.nMinute, time 1.nSecond); Console.WriteLine(time 2的时间为:0时1分钟2秒, time 2.nHour, time 2.nMinute, time 2.nSecond); Console.WriteLine(time 3的时间为:0时1分钟2秒, time 3.nHour, time 3.nMinute, time 3.nSecond); Console.WriteLine(time 4的时间为:0时1分钟2秒, time 4.nHour, time 4.nMinute, time 4.nSecond); 4 方法的参数类型。分别使用值参数,引用参数,输出参数和参数数组编写方法。public class Student public string strName;/姓名 public int nAge;/年龄 public System.Collections.ArrayList strArrHobby = new System.Collections.ArrayList();/爱好/ 构造函数 public Student(string _strName, int _nAge) this.strName = _strName; this.nAge = _nAge; /长大_nSpan岁 public void Grow(int _nSpan, out int _nOutCurrentAge) /输出参数 nAge += _nSpan; _nOutCurrentAge = nAge; / 为爱好赋值 public void SetHobby(params string _strArrHobby) /参数数组 for (int i = 0; i _strArrHobby.Length; i+) this.strArrHobby.Add(_strArrHobbyi); Public static void Swap1(ref int x, ref int y) /应用参数 int tmp; tmp = x; x = y; y = tmp; Public static void Swap2(int x, int y) /值参数 int k; k = x; x = y; y = k; class Test static void Main(string args) Console.writeLine(“这里输出你的学号”); int a = 8, b = 10;Console.WriteLine(a=0,b=1, a, b);Student.Swap2(a,b);Console.WriteLine(a=0,b=1, a, b);Student.Swap1(ref a,ref b);Console.WriteLine(a=0,b=1, a, b); Student s = new Student(张三, 20); int nCurrentAge; s.Grow(3, out nCurrentAge); Console.WriteLine(s.nAge); /输出23 s.SetHobby(游泳, 篮球, 足球); for (int i = 0; i s.strArrHobby.Count; i+) Console.WriteLine(s.strArrHobbyi); 备注:尝试修改一下方法,实验方法的重载。5 运算符重载及字段和属性练习。 class Space private int x; public int y, z; public Space(int xx, int yy, int zz) x = xx; y = yy; z = zz; Public int NX /自定义属性NX set x = value; get return x; public static Space operator (Space d1) /运算符重载 Space Neg = new Space(0, 0, 0); Neg.x = (-1) * d1.x; Neg.y = (-1) * d1.y; Neg.z = (-1) * d1.z; return

温馨提示

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

评论

0/150

提交评论