首页 C#实验7 面向对象程序设计

C#实验7 面向对象程序设计

举报
开通vip

C#实验7 面向对象程序设计实验7面向对象程序设计基础一.实验题目:面次对象程序设计基础二.目的和要求1.掌握类和对象的使用2.掌握类的继承3.掌握构造函数和析构函数的使用4.掌握(静态)方法,属性的使用5.掌握方法的各种参数类型使用方法。三.实验内容(注:本实验所建程序全部都是控制台程序)1.定义一个学生类Student,并实验Private,public等修饰符的功能,实验对象的实例化过程。usingSystem;namespaceExample_PublicAndPrivate{//定义一个学生类publicclassStudent{/...

C#实验7 面向对象程序设计
实验7面向对象程序设计基础一.实验题目:面次对象程序设计基础二.目的和 要求 对教师党员的评价套管和固井爆破片与爆破装置仓库管理基本要求三甲医院都需要复审吗 1.掌握类和对象的使用2.掌握类的继承3.掌握构造函数和析构函数的使用4.掌握(静态)方法,属性的使用5.掌握方法的各种参数类型使用方法。三.实验内容(注:本实验所建程序全部都是控制台程序)1.定义一个学生类Student,并实验Private,public等修饰符的功能,实验对象的实例化过程。usingSystem;namespaceExample_PublicAndPrivate{//定义一个学生类publicclassStudent{//属性publicstringstrName;//公有属性privateintnAge;//私有属性//方法...publicvoidSetAge(int_nAge){this.nAge=_nAge;}}//Main函数类classTest{///应用程序的主入口点。staticvoidMain(string[]args){Console.writeLine(“这里输出你的学号”);Students=newStudent();s.strName="张三“;//正确与否,原因s.nAge=20;//正确与否?原因s.SetAge(20);//赋值年龄Console.WriteLine(s.GetAge());//获取年龄}}}2.类的继承。在1中实现的学生类的基础上,使用继承机制,设计一个大学生类,要求大学生类拥有年龄,姓名和系别属性。usingSystem;namespaceExample_Inheritance{///学生类publicclassStudent{publicstringstrName;//姓名publicintnAge;//年龄}///大学生类:继承学生类publicclassCollegeStudent:Student{publicstringstrInsititute;//所在系}publicclassMainClass{///主函数staticvoidMain(string[]args){Console.writeLine(“这里输出你的学号”);Students=newStudent();s.strName="xiaobao";s.nAge=18;Console.WriteLine("姓名:{0},年龄{1}",s.strName,s.nAge);//使用子类Console.WriteLine("-------------使用子类-------------");CollegeStudentc=newCollegeStudent();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()iscalled.”.classTime{publicintnHour,nMinute,nSecond;publicTime(){nHour=nMinute=nSecond=0;}publicTime(intHour){nHour=Hour;nMinute=nSecond=0;}publicTime(intHour,intMinute){nHour=Hour;nMinute=Minute;nSecond=0;}publicTime(intHour,intMinute,intSecond){nHour=Hour;nMinute=Minute;nSecond=Second;}Public~Time(){Console.WriteLine(“~Time()iscalled“);}}classTest{staticvoidMain(){Console.writeLine(“这里输出你的学号”);Timetime1,time2,time3,time4;//对time1,time2,time3,time4分别调用不同的构造函数time1=newTime();time2=newTime(10);time3=newTime(10,30);time4=newTime(10,30,30);Console.WriteLine("time1的时间为:{0}时{1}分钟{2}秒",time1.nHour,time1.nMinute,time1.nSecond);Console.WriteLine("time2的时间为:{0}时{1}分钟{2}秒",time2.nHour,time2.nMinute,time2.nSecond);Console.WriteLine("time3的时间为:{0}时{1}分钟{2}秒",time3.nHour,time3.nMinute,time3.nSecond);Console.WriteLine("time4的时间为:{0}时{1}分钟{2}秒",time4.nHour,time4.nMinute,time4.nSecond);}}}4.方法的参数类型。分别使用值参数,引用参数,输出参数和参数数组编写方法。publicclassStudent{publicstringstrName;//姓名publicintnAge;//年龄publicSystem.Collections.ArrayListstrArrHobby=newSystem.Collections.ArrayList();//爱好//构造函数publicStudent(string_strName,int_nAge){this.strName=_strName;this.nAge=_nAge;}///长大_nSpan岁publicvoidGrow(int_nSpan,outint_nOutCurrentAge)//输出参数{nAge+=_nSpan;_nOutCurrentAge=nAge;}///为爱好赋值publicvoidSetHobby(paramsstring[]_strArrHobby)//参数数组{for(inti=0;i<_strArrHobby.Length;i++)this.strArrHobby.Add(_strArrHobby[i]);}PublicstaticvoidSwap1(refintx,refinty)//应用参数{inttmp;tmp=x;x=y;y=tmp;}PublicstaticvoidSwap2(intx,inty)//值参数{intk;k=x;x=y;y=k;}}classTest{staticvoidMain(string[]args){Console.writeLine(“这里输出你的学号”);inta=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(refa,refb);Console.WriteLine("a={0},b={1}",a,b);Students=newStudent("张三",20);intnCurrentAge;s.Grow(3,outnCurrentAge);Console.WriteLine(s.nAge);//输出23s.SetHobby("游泳","篮球","足球");for(inti=0;i
本文档为【C#实验7 面向对象程序设计】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: ¥6.6 已有0 人下载
最新资料
资料动态
专题动态
个人认证用户
兽医小王子
暂无简介~
格式:pdf
大小:193KB
软件:PDF阅读器
页数:6
分类:
上传时间:2023-10-12
浏览量:4