下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、面向对象程序设计编程上机作业题目:(1) 调试构造函数pen()例题using system;using system.collections.generic;using system.text;namespace consoleapplication2 class pen public string color; private int price; public pen() color=black; price=5; public void setprice(int newprice) price=newprice; public int getprice() return price;
2、public void setcolor(string newcolor) color=newcolor; public string getcolor() return color; class test public static void main()pen mypen=new pen() ;console.writeline(the price is 0,mypen.getprice();console.writeline(the color is 0,mypen.color); (2) 调试构造函数重载pen()例题using system;class pen public stri
3、ng color; private int price; public pen() color = black; price = 5; public pen(string newcolor, int newprice) color = newcolor; price = newprice; public void setprice(int newprice) price = newprice; public void setprice() price = 5; public int getprice() return price; public void setcolor(string new
4、color) color = newcolor; public string getcolor() return color; class test public static void main() 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.get
5、price(); 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 public class app public int app1(int x
6、) return x * x; public double app1(double y) return y * y; static void main(string args) app x = new app(); console.writeline(5的平方是0, x.app1(5); console.writeline(5.65的平方是0, x.app1(5.65); (4)将运算符+和- -进行重载,使之可对字符型数据进行运算,即:对一个字符型变量a,当a=l时a+(或+a)的值为m, a- -(或- -a)的值为kusing system;using system.collection
7、s.generic;using system.text;namespace consoleapplication6 public class chartest private char ch; public chartest() this.ch = ; public chartest(char val) this.ch = val; public char c get return this.ch; set this.ch = value; static public chartest operator +(chartest orig) chartest result = new charte
8、st(); result.ch = (char)(orig.ch + 1); return result; static public chartest operator -(chartest orig) chartest result = new chartest(); result.ch = (char)(orig.ch - 1); return result; public class overloadapp public static void main() chartest a = new chartest(l); chartest b = new chartest(l); cons
9、ole.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类型的参数,在此构造函数中将传递的字符串打印出来;在main方法中创建属于test1类
10、的一个对象,不传递参数;在main方法中创建属于test1类的另一个对象,传递一个字符串“this is a string”;using system;using system.collections.generic;using system.text;namespace consoleapplication8 class program class test1 public string test; public test1() test = test1; public test1(string s) test = s; static void main(string args) strin
11、g 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和类
12、b的对象,并分别调用mymethod方法;using system;class classa public static int mymethod(int i) i=i+10; return i;class classb:classapublic static int mymethod(int i)i=i+50;return i;class testpublic static void main() classa a=new classa(); classb b=new classb(); console.writeline(请输入一个数:); string s=console.readlin
13、e(); 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 appdelegate int process(int a,int b); public static int add(int a,int b) return a+b; public static int sub(int a,int b) return a-b;static void main() string s,s1,s2; process pro; console.writeline(请输入一个数:); s1=console.readline(); int a=int32.parse(s1); console.wr
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年医美填充智能材料项目营销方案
- 2026年人工智能 交通项目投资计划书
- 2026年储能安全预警与防护项目营销方案
- 2026年AI原生算力基础设施项目营销方案
- 2026江西南昌市消防救援局首次面向社会招聘消防文员4人备考题库含答案详解(夺分金卷)
- 第一季度广东广州市客都文创产业孵化(梅州)有限公司招聘备考题库附参考答案详解(满分必刷)
- 2026年在线高精导航系统项目可行性研究报告
- 2026江西省肿瘤医院高层次人才招聘29人备考题库附答案详解(黄金题型)
- 2026黑龙江省交通投资集团有限公司面向社会招聘10人备考题库附参考答案详解(研优卷)
- 2026江西事业单位联考赣州市招聘1170人备考题库及答案详解(网校专用)
- 2026春节后复工复产安全培训第一课
- 2026湖南衡阳日报社招聘事业单位人员16人备考题库完整参考答案详解
- 2026年山东药品食品职业学院单招综合素质考试备考试题含详细答案解析
- GB/T 46878-2025二氧化碳捕集、运输和地质封存地质封存
- 2026年1月浙江省高考(首考)历史试题(含答案)
- 借款合同2026年担保协议
- 征兵体检培训课件
- 2024年河北省中考化学真题及答案解析
- 2025年职业卫生试题试题及答案
- 消毒供应室职业暴露防范
- 2025年内蒙古行政执法考试试题及答案
评论
0/150
提交评论