2022年面向对象程序设计编程实验上机.docx
精选学习资料 - - - - - - - - - 面对对象程序设计编程上机作业题目:(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 ,myPen.GetPrice; Pen myPen=newPen ;Console .WriteLine"The price is 0"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 名师归纳总结 - - - - - - -第 1 页,共 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 , myPen.GetPrice; Pen myPen = newPen ;Pen hisPen = newPen "GREEN", 8 ;Console .WriteLine"The price is 0"Console .WriteLine"The Color is 0", myPen.Color;Console .WriteLine"The price is 0", hisPen.GetPriceConsole .WriteLine"The Color is 0", hisPen.GetColor; hisPen.SetPrice;, hisPen.GetPrice;Console .WriteLine"The price is 0" (3)使用重载方法求 5 和 5.65 的平方;using System ;using System.Collections.Generic;using System.Text;namespace ConsoleApplication8 class Program publicclass 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)将运算符 +和 - - 进行重载,使之可对字符型数据进行运算,即:对一个字符型变量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 = value ; staticpublic charTest operator + charTest orig charTest Result = newcharTest ; Result.ch = char orig.ch + 1;return Result; staticpublic charTest operator - charTest orig charTest Result = newcharTest ; Result.ch = char orig.ch - 1;return Result; publicclass overloadApp publicstaticvoid Main charTest a = newcharTest 'L' ;charTest b = newcharTest '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 名师归纳总结 - - - - - - -第 3 页,共 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 = string"Test1" ; public Test1 s test = s; staticvoidMain string args string x = "This is a string";Test1 app1 = newTest1 ;Test1 app2 = newTest1 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 名师归纳总结 - - - - - - -第 4 页,共 5 页精选学习资料 - - - - - - - - - publicstaticvoid Main ClassA a= newClassA ;ClassB b= newClassB ;Console .WriteLine " 请输入一个数:" ;string s= Console .ReadLine;int x= Int32 .Parses;Console .WriteLine " 调用 A类方法: 0" , ClassA .MyMethodx;Console .WriteLine " 调用 B类方法: 0" , ClassB .MyMethodx; (7)利用托付实现:当用户输入一个字符1时,调用两个整数相加的方法,运算两整数之和;当用户输入一个字符2时,调用两个整数相减的方法,运算两整数之差;using System ;class App delegateint process 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,s2process Pro ;Console .WriteLine " 请输入一个数:" ; s1= Console .ReadLine;int a= Int32 .Parses1;Console .WriteLine " 请输入另一个数:" ; s2= Console .ReadLine;int b= Int32 .Parses2;Console .WriteLine " 请挑选运算方法:为相加,为相减 " ; s= Console .ReadLine;if s= "1" Pro= newprocess add ;else Pro= newprocess sub ;Console .WriteLine" 结果为: 0" ,Proa,b; 5 / 5 名师归纳总结 - - - - - - -第 5 页,共 5 页