




已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年漳州两家企业招聘真题
- 供配电工程知识培训总结课件
- 特种设备(锅炉、压力容器)培训考试试题及答案
- 临床思维培训-消化系统专题考核试题与答案
- 2025年方山县关于市场主体倍增工程的会议记录
- 2025年碳纤维针刺预制件合作协议书
- 2025年医院精神科噎食患者应急预案及演练脚本
- 2025年铸造造型材料项目提案报告
- 2025年表面改性金属材料合作协议书
- 2026届清华大学中学生标准学术能力诊断性测试化学高二上期中质量检测模拟试题含解析
- 台球厅消防知识培训课件
- 2025便利店加盟的合同样本
- 评职称老师考试题目及答案
- 2025年内分泌风湿免疫科进修人员出科理论考试试题及答案
- 2025至2030中国防砸安全鞋行业运营态势与投资前景调查研究报告
- 学堂在线 高技术与现代局部战争 章节测试答案
- 2025年医疗器械仓库管理培训试题及答案
- 助焊剂存储管理办法
- 乙型肝炎防治知识教学课件
- 环卫安全事故处理方案
- 七十岁老年人三力测试驾考题库
评论
0/150
提交评论