2022年面向对象程序设计编程实验上机.docx
《2022年面向对象程序设计编程实验上机.docx》由会员分享,可在线阅读,更多相关《2022年面向对象程序设计编程实验上机.docx(5页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选学习资料 - - - - - - - - - 面对对象程序设计编程上机作业题目:(1)调试构造函数 Pen()例题using System ;using System.Collections.Generic;using System.Text;namespace ConsoleApplication2 class Pen publicstring Color ;privateint Price ;public Pen Color= BLACK; Price=5; publicvoid SetPrice int newPrice Price=newPrice; publicint GetPr
2、ice return Price ; publicvoid SetColor string newColor Color=newColor ; publicstring GetColor return Color ; class Test publicstaticvoidMain ,myPen.GetPrice; Pen myPen=newPen ;Console .WriteLineThe price is 0Console .WriteLineThe Color is 0,myPen.Color; (2)调试构造函数重载 Pen()例题using System ;class Pen pub
3、licstring Color ;privateint Price ;public Pen Color = BLACK; Price = 5; public Pen string newColor, int newPrice Color = newColor; Price = newPrice;1 / 5 名师归纳总结 - - - - - - -第 1 页,共 5 页精选学习资料 - - - - - - - - - publicvoid SetPrice int newPrice Price = newPrice; publicvoid SetPrice Price = 5; publicin
4、t GetPrice return Price; publicvoid SetColor string newColor Color = newColor; publicstring GetColor return Color ; class Test publicstaticvoidMain , myPen.GetPrice; Pen myPen = newPen ;Pen hisPen = newPen GREEN, 8 ;Console .WriteLineThe price is 0Console .WriteLineThe Color is 0, myPen.Color;Consol
5、e .WriteLineThe price is 0, hisPen.GetPriceConsole .WriteLineThe Color is 0, hisPen.GetColor; hisPen.SetPrice;, hisPen.GetPrice;Console .WriteLineThe price is 0 (3)使用重载方法求 5 和 5.65 的平方;using System ;using System.Collections.Generic;using System.Text;namespace ConsoleApplication8 class Program public
6、class app publicint app1 int x return x * x; publicdouble app1 double y return y * y; staticvoid Main string args app x = newapp ;Console .WriteLine 5 的平方是 0 , x.app15;Console .WriteLine 5.65 的平方是 0 , x.app15.65; 2 / 5 名师归纳总结 - - - - - - -第 2 页,共 5 页精选学习资料 - - - - - - - - - (4)将运算符 +和 - - 进行重载,使之可对字
7、符型数据进行运算,即:对一个字符型变量a,当 a=L时 a+或+a的值为M, a- -或- -a的值为Kusing System ;using System.Collections.Generic;using System.Text;namespace ConsoleApplication6 publicclass charTest privatechar ch ;public charTest this .ch = ; public charTest char val this .ch = val; publicchar c get returnthis .ch ; set this .ch
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022 面向 对象 程序设计 编程 实验 上机
限制150内