c#实习2报告

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

实验报告实验二:抽象类、抽象方法、密封类、密封方法实验目的:1.掌握抽象类的定义方法。2.掌握抽象方法的调用方法。3.了解密封类、密封方法的使用。实验内容:一、1.运行课本例8.6,仔细理解抽象方法的定义及调用方法。注:此题不必给出程序代码,只需给出运行结果,并写出抽象方法的定义要点。2.定义一个车辆(Vehicle)基类;然后以该类为基类,派生出bicycle、car等类,并编程对该派生类的功能进行验证。具体要求如下:类Vehicle有三个字段:Speed、MaxSpeed、Weight。类Vehicle有一个构造函数,其功能是通过参数设置字段Speed、MaxSpeed、Weight的值。为派生类bicycle添加一个新字段Price,并设计构造函数,在构造函数中通过参数设置字段Speed、MaxSpeed、Weight、Price的值。并在验证时调用这些该构造函数,测试用例:Speed=10、MaxSpeed=30、Weight=20000、Price=1000。为派生类car添加一个新字段Price,并设计构造函数,在构造函数中通过参数设置字段Speed、MaxSpeed、Weight、Price的值。并在验证时调用这些该构造函数,测试用例:Speed=60、MaxSpeed=150、Weight=3000000、Price=100000。Vehicle有两个虚方法Run和Stop。Run方法显示“一辆车在马路上奔驰。”,Stop方法显示“一辆车在马路上停了下来。”。派生类bicycle的Run方法显示“一辆自行车在马路上奔驰。”,Stop方法显示“一辆自行车在马路上停了下来。”。派生类car的Run方法显示“一辆汽车在马路上奔驰。”,Stop方法显示“一辆汽车在马路上停了下来。”。对派生类的功能进行验证。提示:验证bicycle类的代码可如下所示:验证结果类似下图:实验报告3.现要将第2题中Vehicle的虚方法Run和Stop改为抽象方法,应该怎样修改?请写出修改后的类Vehicle的定义。4.写出下面程序的运行结果并分析其原因。实验数据记录及分析(或程序及运行结果)第一题:实验报告抽象类的要点:抽象类不能直接实例化,使用new会导致编译错误。从抽象类派生非抽象类时,这些非抽象类必须实现所继承的所有抽象成员。第二题:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceConsoleApplication1{publicclassVehicle{publicdoublespeed;publicdoublemaxspeed;publicdoubleweight;publicVehicle(doublespeed,doublemaxspeed,doubleweight){this.speed=speed;this.maxspeed=maxspeed;this.weight=weight;}publicvirtualstringrun(){return一辆车在马路上奔驰;}publicvirtualstringstop(){return一辆车在马路上停了下来;}}publicclassBicycle:Vehicle{publicdoublebicycleprice;实验报告publicBicycle(doublespeed,doublemaxspeed,doubleweight,doubleprice):base(speed,maxspeed,weight){this.bicycleprice=price;}publicoverridestringrun(){return一辆自行车在马路上奔驰;}publicoverridestringstop(){return一辆自行车在马路上停了下来;}}publicclassCar:Vehicle{publicdoublecarprice;publicCar(doublespeed,doublemaxspeed,doubleweight,doubleprice):base(speed,maxspeed,weight){this.carprice=price;}publicoverridestringrun(){return一辆小汽车在马路上奔驰;}publicoverridestringstop(){return一辆小汽车在马路上停了下来;}}publicclassTextVehicle{staticvoidMain(string[]args){Bicycleobjbicycle=newBicycle(10,30,2000,1000);Console.WriteLine(自行车的速度是{0},最大速度是{1},重量是{2},价格是{3},objbicycle.speed,objbicycle.maxspeed,objbicycle.weight,objbicycle.bicycleprice);Console.WriteLine({0},objbicycle.run());Console.WriteLine({0},objbicycle.stop());Carobjcar=newCar(60,150,3000000,100000);Console.WriteLine(小汽车的速度是{0},最大速度是{1},重量是{2},价格是{3},objcar.speed,objcar.maxspeed,objcar.weight,objcar.carprice);Console.WriteLine({0},objcar.run());Console.WriteLine({0},objcar.stop());Console.ReadKey();实验报告Console.ReadLine();}}}第三题:将publicclassVehicle改为publicabstractclassVehicle定义为抽象方法将Vehicle的run和stop中的{return一辆自行车在马路上奔驰;}和{return一辆车在马路上停了下来;}改为{return}和{return}将publicvirtualstringrun()改为publicoverridevoidrun();将stop方法也同样改写即可。第四题:实验报告原因:override是重写方法,new是覆盖方法。B是A的派生类在B中重写了A中的方法F,覆盖了A中的方法G。评语:日期:年月日

1 / 6
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功