欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

    2022年面向对象程序设计编程实验上机 .pdf

    • 资源ID:25468168       资源大小:29.92KB        全文页数:5页
    • 资源格式: PDF        下载积分:4.3金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要4.3金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    2022年面向对象程序设计编程实验上机 .pdf

    1 / 5 面向对象程序设计编程上机作业题目:(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 GetPrice() return Price 。 publicvoid SetColor(string newColor) Color=newColor 。 publicstring GetColor() return Color 。 class Test publicstaticvoidMain() Pen myPen=newPen() 。Console .WriteLine(The price is 0,myPen.GetPrice()。Console .WriteLine(The Color is 0,myPen.Color)。 (2)调试构造函数重载 Pen()例题using System 。class Pen publicstring Color 。privateint Price 。public Pen() Color = BLACK。 Price = 5。 public Pen( string newColor, int newPrice) Color = newColor。 Price = newPrice。精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 1 页,共 5 页2 / 5 publicvoid SetPrice(int newPrice) Price = newPrice。 publicvoid SetPrice() Price = 5。 publicint GetPrice() return Price。 publicvoid SetColor(string newColor) Color = newColor。 publicstring GetColor() return Color 。 class Test publicstaticvoidMain() Pen myPen = new Pen() 。Pen hisPen = new Pen( GREEN , 8) 。Console .WriteLine(The price is 0, myPen.GetPrice()。Console .WriteLine(The Color is 0, myPen.Color)。Console .WriteLine(The price is 0, hisPen.GetPrice()。Console .WriteLine(The Color is 0, hisPen.GetColor()。 hisPen.SetPrice()。Console .WriteLine(The price is 0, hisPen.GetPrice()。 (3)使用重载方法求5 和 5.65 的平方。using System 。using System.Collections.Generic。using System.Text。namespace ConsoleApplication8 class Program publicclassapp publicint app1( int x) return x * x。 publicdouble app1( double y) return y * y。 staticvoidMain( string args) app x = newapp() 。Console .WriteLine(5 的平方是 0 , x.app1(5)。Console .WriteLine(5.65 的平方是 0 , x.app1(5.65)。 精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 2 页,共 5 页3 / 5 (4)将运算符 +和- - 进行重载,使之可对字符型数据进行运算,即:对一个字符型变量a,当 a=L时 a+(或+a)的值为 M , a- -(或- -a)的值为 Kusing System 。using System.Collections.Generic。using System.Text。namespace ConsoleApplication6 publicclasscharTest privatechar ch 。public charTest() this .ch = 。 public charTest(char val) this .ch = val。 publicchar c get returnthis.ch 。 set this .ch = value 。 staticpubliccharTest operator +( charTest orig) charTest Result = newcharTest () 。 Result.ch = (char )(orig.ch + 1)。return Result。 staticpubliccharTest operator -(charTest orig) charTest Result = newcharTest () 。 Result.ch = (char )(orig.ch - 1)。return Result。 publicclassoverloadApp publicstaticvoidMain() charTest a = newcharTest ( L ) 。charTest b = new charTest ( L ) 。Console .WriteLine(Original value is :0,1, a.c, b.c)。 a = +a。 b = -b。Console .WriteLine(Current value is :0.1, a.c, b.c)。 a = +a。 b = -b。Console .WriteLine(Final value is:0,1, a.c, b.c)。 (5)编写一个控制台程序,完成下列功能并输出结果:创建一个类 Test1 ,用无参数的构造函数输出该类的类名;增加一个重载的构造函数,带有一个string类型的参数,在此构造函数中将传递的字符精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 3 页,共 5 页4 / 5 串打印出来;在 Main 方法中创建属于Test1 类的一个对象,不传递参数; 在 Main 方 法中 创 建 属于 Test1类 的另 一个 对 象 ,传 递一 个 字 符串 “This is a string”;using System 。using System.Collections.Generic。using System.Text。namespace ConsoleApplication8 class Program class Test1 publicstring test。public Test1() test = Test1 。 public Test1(string s) test = s。 staticvoidMain( string args) string x = This is a string。Test1 app1 = new Test1 () 。Test1 app2 = new Test1 (x) 。Console .WriteLine(0 , app1.test)。Console .WriteLine(0 , app2.test)。 (6)编写一个控制台程序,完成下列功能并输出结果:创建一个类 A,在 A中编写一个可以被重写的带int类型参数的方法 MyMethod,并在该方法中输出传递的整型值加10 后的结果;再创建一个类B,使其继承自类A,然后重写 A中的 MyMethod方法, 将 A中接收的整型值加 50,并输出结果;在 Main 方法中分别创建类A和类 B的对象,并分别调用MyMethod方法;using System 。class ClassA publicstaticint MyMethod( int i) i=i+10。 return i 。 class ClassB : ClassA publicstaticint MyMethod( int i) i=i+50。return i 。 class Test 精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 4 页,共 5 页5 / 5 publicstaticvoidMain() ClassA a=newClassA() 。ClassB b= newClassB () 。Console .WriteLine(请输入一个数: ) 。string s= Console .ReadLine()。int x= Int32 .Parse(s)。Console .WriteLine(调用 A类方法: 0 , ClassA .MyMethod(x)。Console .WriteLine(调用 B类方法: 0 , ClassB .MyMethod(x)。 (7)利用委托实现:当用户输入一个字符1时,调用两个整数相加的方法,计算两整数之和;当用户输入一个字符2时,调用两个整数相减的方法,计算两整数之差。using System 。class App delegateintprocess ( int a, int b) 。publicstaticint add( int a, int b) return a+b 。 publicstaticint sub( int a, int b) return a-b 。 staticvoidMain() string s,s1,s2。process Pro 。Console .WriteLine(请输入一个数: ) 。 s1=Console .ReadLine()。int a= Int32 .Parse(s1)。Console .WriteLine(请输入另一个数: ) 。 s2=Console .ReadLine()。int b= Int32 .Parse(s2)。Console .WriteLine(请选择运算方法:为相加,为相减 ) 。 s=Console .ReadLine()。if (s= 1 ) Pro= newprocess (add) 。else Pro= newprocess (sub) 。Console .WriteLine(结果为: 0 ,Pro(a,b)。 精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 5 页,共 5 页

    注意事项

    本文(2022年面向对象程序设计编程实验上机 .pdf)为本站会员(Q****o)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

    工信部备案号:黑ICP备15003705号 © 2020-2023 www.taowenge.com 淘文阁 

    收起
    展开