2013面向对象程序设计复习题文档.doc

上传人:wux****ua 文档编号:8865959 上传时间:2020-04-01 格式:DOC 页数:16 大小:100.50KB
返回 下载 相关 举报
2013面向对象程序设计复习题文档.doc_第1页
第1页 / 共16页
2013面向对象程序设计复习题文档.doc_第2页
第2页 / 共16页
2013面向对象程序设计复习题文档.doc_第3页
第3页 / 共16页
点击查看更多>>
资源描述
面向对象程序设计考试题型:1) 选择题10题,共20分2) 填空题10题,共20分3) 程序阅读题3题,共15分4) 程序填空题,10空,共20分5) 简答题3题,共15分6) 编程题1题,共10分一、 程序阅读题和程序填空题输入任意两个整数,交换后输出。using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace FUXU1 class Program static void Main(string args) int a, b, c; string strInput; Console.WriteLine(Please input two numbers); strInput = Console.ReadLine(); string strValues = strInput.Split(); a = int.Parse(strValues0); b = int.Parse(strValues1);接受数据的通用方法。 Console.WriteLine(a=0,b=1, a, b); c = a; a = b; b = c; Console.WriteLine(After changing:); Console.WriteLine(a=0,b=1, a, b); Console.ReadKey(); 从键盘上输入三个整数,输出其中的最大数。using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace FUXI2 class Program static void Main(string args) int a, b, c, max; string strInput; string strValues; Console.WriteLine(Input a,b,c:); strInput = Console.ReadLine(); strValues = strInput.Split( );拆分字符串,通过空格分割,一个一个输入 a = int.Parse(strValues0); b = int.Parse(strValues1); c = int.Parse(strValues2); Console.WriteLine(a=0,b=1,c=2, a, b, c); if (a b) if (a c) max = a; else max = c; else if (b c) max = b; else max = c; Console.WriteLine(The biggest number is:); Console.WriteLine(max=0, max); Console.ReadKey(); 从整数半径为1的圆开始,输出圆周长,直到周长大于139.67时停止using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace FUXI3 class Program const double PI = 3.1415926; static void Main(string args) int r; double Zhouchang; Console.WriteLine(Please input the r of the circle); for (r = 1; true; r+) Zhouchang = PI * 2 * r; if (Zhouchang 139.67) break; Console.WriteLine(r=0,Zhouchang=1, r, Zhouchang ); Console.ReadKey(); 求输入的10个整数中偶数的个数及其和值using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace FUXI4 class Program static void Main(string args) int i, num = 0, a; double sum = 0; Console.WriteLine(Input 10 integer:); for (i = 0; i b ? a : b;如果a大于b,那么返回a;若a小于或等于b 则返回b 。? : 三目运算符。a b?a:b 表示的是:if (ab ) return a ;else return b 问号前的表达式若为真时 ,则选取冒号前的值,否则选冒号后的 public double GetThreeMax(double a, double b, double c)输入和获取三个双精度浮点数 double x = GetTwoMax(a, b); return GetTwoMax(x, c); 主程序:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace FUXI5 public class Program static void Main(string args) Max Max = new Max(); Console.WriteLine(Two numbers are 11.22 and 1.31); Console.WriteLine(Twomax = 0,Max.GetTwoMax(11.22, 1.31)数字可以自行修改的,下面三个的时候相同。); Console.WriteLine(Three numbers are 11.22,1.31 and 2.25); Console.WriteLine(Threemax = 0,Max.GetThreeMax(11.22, 1.31, 2.25); 定义一个类,提供成员方法分别求两个整数、三个双精度浮点数、四个单精度浮点数中的较小数。类:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace FUXI6 class Min public int GetTwoMin(int a, int b) return a b ? a : b; public double GainTwoMin(double a, double b) return a b ? a : b; public double GetThreeMin(double a, double b, double c) double x = GainTwoMin(a, b); return GainTwoMin(x, c); 三个双精度数求最小,先用两个进行比较,当进行三个数字求最小的时候,先两个数字比较,即对上一方法进行调用。 public float TwoMin(float a, float b) return a b ? a : b; public float GetFourMin(float a, float b, float c, float d) float x; x = TwoMin(a, b); x= TwoMin(c, x); x = TwoMin(d, x); return x; 四个双精度数求最小,先建立用两个进行比较的方法,然后两两比较,即对上一方法进行调用。 主函数:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace FUXI6 class Program static void Main(string args) Min Min = new Min(); Console.WriteLine(Twomin = 0,Min.GetTwoMin(11, 12); Console.WriteLine(Threemin = 0,Min.GetThreeMin(23.11, 12.77, 99.99); Console.WriteLine (Fourmin=0,Min.GetFourMin(11.2f,1.3f,2.2f,9.7f)C#程序中输入的数字默认为double形式,所以对于单精度数字要有f,不然虽然也可以运行可是会有警告。); 定义一个矩形类,该类包含左上角位置、长和宽性质,一个无参构造函数、一个有参构造函数以及一个静态属性和一个静态方法来统计创建的矩形对象的个数。矩形类:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace FUXI7 public class JUXING public int _x; public int _y; public int _chang; public int _kuan; public static int num1 = 0;静态数据成员,类所拥有的成员,即所有成员共享一个。在此矩形类中是所有数据共享一个计算一共创建几个矩形的数。定义格式:访问修饰符 static 类型 数据成员名称 初始值;访问方法: 类名.静态数据成员 public static int num() return num1; 静态属性定义格式:访问修饰符 static 类型 属性名称 get set;访问方法: 类名.静态属 public JUXING() _x =1; _y =1; _chang =3; _kuan =2; num1 += 1; 构造有参函数,此处需要对所有的数进行赋值。 public JUXING(int x,int y,int chang,int kuan) _x = x; _y = y; _chang = chang; _kuan = kuan; num1 += 1; 构造无参函数,此处不需要赋值,其赋值工作将在主函数里进行操作。 主函数:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace FUXI7 class Program static void Main(string args) JUXING ju1, ju2, ju3; ju1 = new JUXING ();构造函数通过new来调用,因为此处是对有参函数的调用,所以调用括号里不需要赋值 Console.WriteLine( 左上角位置:(0,1) 长:2 宽:3 创建个数:4,ju1._x, ju1._y,ju1 ._chang ,ju1 ._kuan , JUXING.num(); ju2 = new JUXING(2, 2, 4, 2);构造函数通过new来调用,无参函数调用需要赋值的,下同。 Console.WriteLine(左上角位置:(0,1) 长:2 宽:3 创建个数:4, ju2._x, ju2._y, ju2._chang,ju2 ._kuan , JUXING.num(); ju3 = new JUXING(6, 3, 9, 7);构造函数通过new来调用 Console.WriteLine(左上角位置:(0,1) 长:2 宽:3 创建个数:4, ju3._x, ju3._y, ju3._chang, ju3._kuan, JUXING.num(); Console.WriteLine(一共创建了矩形:); Console.WriteLine(JUXING num = 0, JUXING.num(); Console.ReadKey(); 已知有图形三角形和正方形,三角形有三个顶点的位置、填充颜色等属性,有计算面积和周长的方法;正方形有中心点的位置、填充颜色、边长等属性,有计算面积和周长的方法。要求创建一个图形的基类并采用抽象类和抽象方法的方式来实现求面积、周长以及显示图形具体信息的功能。类:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace FUXI8 public abstract class SHAPE创建一个抽象基类。 public int _x; public int _y; public string _color;基类的共有属性。 public SHAPE(int x, int y, string color) _x = x; _y = y; _color = color; 无参方法 public SHAPE() _x = 0; _y = 0; _color = 黑色; 有参方法 public abstract double CalArea(); public abstract double CalAround(); public abstract string ShowInfo();均为基类 注意:抽象类只能作为基类使用,不能创建对象。 public class triangle : SHAPE三角形派生类 public int _a; public int _b; public int _c; public triangle() _a = 0; _b = 0; _c = 0; public triangle(int x, int y, string color, int a, int b, int c) : base(x, y, color)构造派生类对象。通过构造函数给基类构造函数传递参数,其格式如下:访问修饰符派生类构造函数(形参列表):base(传递给基类构造函数的实参列表) 方法体 下同 _a = a; _b = b; _c = c; public override double CalArea() return Math.Sqrt(_a + _b + _c) / 2 * (_a + _b + _c) / 2 - _a) * (_a + _b + _c) / 2 - _b) * (_a + _b + _c) / 2 - _c); public override double CalAround() return _a + _b + _c; ; public override string ShowInfo() return string.Format(三角形信息:(0,1),2,三边长:345, _x, _y, _color, _a, _b, _c); 虚方法 在C#中,主要通过虚方法来实现多态。 声明虚方法的方法。 基类中的声明格式:public virtual 返回值类型 方法名称(参数列表) 派生类中的声明格式:public override返回值类型 方法名称(参数列表)下同! public class Rect : SHAPE矩形派生类 public int _r; public Rect() _r = 0; public Rect(int x, int y, string color, int r) : base(x, y, color) _r = r; public override double CalArea() return _r * _r; public override double CalAround() return 4 * _r; public override string ShowInfo() return string.Format(正方形信息:(0,1),2, 边长:3 , _x, _y, _color, _r); 主函数:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace FUXI8 public class Program static void MainShowAllInfo(SHAPE shp) Console.WriteLine(0 面积:1 周长:2,shp.ShowInfo(), shp.CalArea(), shp.CalAround(); 输出 static void Main(string args) SHAPE shape; shape = new triangle(40, 50, Blue, 5, 6, 7); MainShowAllInfo(shape); shape = new Rect(70, 80, Green, 20); MainShowAllInfo(shape);赋值 Console.ReadKey();终止程序 平面点的+、运算 类:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace FUXI9 public class Complex private int real; private int imaginary; public Complex(int real, int imaginary) this.real = real; this.imaginary = imaginary; public static Complex operator +(Complex c1, Complex c2) return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary); public static Complex operator -(Complex c1, Complex c2) return new Complex(c1.real - c2.real, c1.imaginary - c2.imaginary); 运算符重载通过运算符重载使类的对象可以象基本数据类型的变量一样直接进行运算。运算符重载就是定义一个以运算符为名称的方法,其实现格式如下:单目运算符定义格式public static 运算结果类型 operator 单目运算符名称(运算量类型 运算量名称) 双目运算符定义格式public static 运算结果类型 operator 双目运算符名称(左边运算量类型左边运算量名称,右边运算量类型 右边运算量名称) 此处是双目运算符 public override string ToString()以string 输出。 if (imaginary 0) return (String.Format(0 1i, real, imaginary); else return (String.Format(0 + 1i, real, imaginary); 主函数:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace FUXI9 class Program static void Main(string args) Complex num1 = new Complex(1, 2); Complex num2 = new Complex(7, 7); Complex sum_Add = num1 + num2; Complex sum_Minus = num1 - num2; Console.WriteLine(First complex number: 0, num1); Console.WriteLine(Second complex number: 0, num2); Console.WriteLine(The sum of the two numbers: 0, sum_Add); Console.WriteLine(The Minus of the two numbers: 0, sum_Minus); Console.ReadLine(); Console.ReadKey(); 采用委托的方式在C#中,允许将一个类中的方法传递给另一个能调用该方法的类的某个对象叫委托。使用委托的步骤是:(1)声明 一个委托类,其参数形式一定要和想要包含的方法的参数形式一致(2)定义所有要定义的方法,其参数形式和第一步中声明的委托类的参数形式必须相同。(3)创建委托对象并将所要的方法包含在该委托对象中(4)通过委托对象调用包含在其中的方法求四个实数里的最大值和三个实数里的最小值。using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace FUXI10 public class Program public delegate int math(int a, int b, int c, int d); public delegate int Math(int a, int b, int c); class compare public int max(int a, int b, int c, int d) int max1; max1 = a; if (max1 b) max1 = b; if (max1 c) max1 = c; if (max1 b) min1 = b; if (min1 c) min1 = c; return min1; class test static void Main() math delegateobj1; Math delegateobj2; compare answer = new compare(); delegateobj1 = new math(answer.max); delegateobj2 = new Math(answer.min); double tt1 = delegateobj1(0, 1, 7, 9); double tt2 = delegateobj2(7, 9, 11); Console.WriteLine(0,1,7,9四个实数中的最大实数是?); Console.WriteLine(7,9,11三个实数中的最小实数是?); Console.WriteLine(答:最大值:0 最小值:1, tt1, tt2); Console.ReadKey(); 从键盘输入个实数存放到数组中,然后按由大到小的顺序排序,在屏幕上输出; 从键盘输入一个实数,如果该数在数组中存在则删除并输出删除后的结果。求5*6矩阵中的最大值及其所在的位置using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace FUXI12 class Program static void Main(string args) int, x = new int6, 5; for (int i = 0; i 6; i+) for (int j = 0; j 5; j+) xi, j = (i + 1) * (j + 1); for (int i = 0; i 6; i+) for (int j = 0; j 5; j+) Console.Write(0,5:d, xi, j); Console.WriteLine(); Console.WriteLine(最大值为30,所在位置是第6行,第5列); Console.ReadLine(); 建立10名学生的信息表,每个学生的数据包括学号、姓名和三门课的成绩,求出每个学生的总分和平均分,在屏幕上把学生信息、总分和平均分输出出来。using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace FUXI13 struct student public string studentid; public string name; public float score1; public float score2; public float score3; public float add; public float average; 构造结构体结构体是一种构造数据类型用途:把不同类型的数据组合成一个整体-自定义数据类型结构体类型定义格式:struct 结构体类型名 public 类型标识符 成员名; public类型标识符 成员名; . public class Program static void Main(string args) student stu = new student10; stu0.studentid = 30; stu0.name = Yao; stu0.score1 = 70; stu0.score2 = 78; stu0.score3 = 80; stu1.studentid = 31; stu1.name = Ou; stu1.score1 = 88; stu1.score2 = 89; stu1.score3 = 92; stu2.studentid = 32; stu2.name = Zhu; stu2.score1 = 85; stu2.score2 = 76; stu2.score3 = 83; stu3.studentid = 33; stu3.name = Wang; stu3.score1 = 76; stu3.score2 = 77; stu3.score3 = 82; stu4.studentid = 34; stu4.name = Cheng; stu4.score1 = 74; stu4.score2 = 73; stu4.score3 = 81; stu5.studentid = 35; stu5.name = Yang; stu5.score1 = 70; stu5.score2 = 68; stu5.score3 = 75; stu6.studentid = 36; stu6.name = Jiang; stu6.score1 = 90; stu6.score2 = 87; stu6.score3 = 95; stu7.studentid = 37; stu7.name = Shi; stu7.score1 = 71; stu7.score2 = 75; stu7.score3 = 89; stu8.studentid = 38; stu8.name = Hu; stu8.score1 = 66; stu8.score2 = 76; stu8.score3 = 83; stu9.studentid = 39; stu9.name = Wang; stu9.score1 = 60; stu9.score2 = 74; stu9.score3 = 72; for (int j = 0; j 10; j+) stuj.add = stuj.score1 + stuj.score2 + stuj.score3; stuj.average = stuj.add / 3; for (int i = 0; i 10; i+) Console.WriteLine(0,3:1,5 分数一:2,1 分数二:3,1 分数三:4,1 总分:5,1 平均分:6,1, stui.studentid, stui.name, stui.score1, stui.score2, stui.score3, stui.add, stui.average); 二、 简答题1) 接口和抽象类的区别是什么?抽象类和接口的区别:1、抽象类可以拥有非抽象成员2、抽象类成员可以是非公有的,接口一般是公有的3、一个子类可以继承多个接口,只能有一个父类2) 使用委托的步骤?使用委托的步骤是:(1)声明 一个委托类,其参数形式一定要和想要包含的方法的参数形式一致(2)定义所有要定义 的方法,其参数形式和第一步中声明的委托类的参数形式必须相同。(3)创建委托对象并将所要的方法包含在该委托对象中(4)通过委托对象调用包含在其中的方法3) 简述 private、 protected、 public、 internal 修饰符的访问权限private:只限于该类成员,其它类不能访问(默认)protected:只限于该类成员和派生类成员访问internal:内部类,不能跨工程访问(默认)public:公有类,访问不受限制sealed:密封类,不能继承4) 主菜单的设计步骤主菜单的设计步骤(1)添加主菜单控件( 2)添加菜单项( 3)移动菜单项( 4)删除菜单项5) 工具栏的设计步骤工具栏的设计步骤(1)添加工具栏控件( 2)设置控件的属性 Dock、ShowItemToolTips、Items( 3)添加工具按钮(ToolStripButton)( 4)设置按钮属性 Enabled、Image、DisplayStyle、Text、ToolTipText( 5)添加按钮事件6) 自定义对话框的分类及各自的特点自定义对话框分为模式对话框和非模式对话框。模式对话框是指用户只能在当前窗体中进行操作,不能切换到其它窗体,非模式对话框是指用户可以自由切换当前窗体。7) 继承的规则是什么?继承的规则: 只能继承一个基类 不能继承构造函数,其它成员都能继承 可以传递 派生类成员由继承过来的基类成员和自己定义的新成员组成 派生类定义的新成员与基类成员同名时,覆盖基类成员8) 虚方法的作用及定义格式?虚方法作用: 在C#中,主要通过虚方法来实现多态。 声明虚方法的方法。 基类中的声明格式:public virtual 返回值类型 方法名称(参数列表) 派生类中的声明格式: public override返回值类型 方法名称(参数列表)9) 面向对象的三大特征是什么?封装(encapsulation): 封装是面向对象方法的一个重要原则。它有两个涵义:第一个涵义是,把对象的全部属性和全部服务结合在一起,形成一个不可分割的独立单位(即对象)。第二个涵义也称作“信息隐蔽”,即尽可能隐蔽对象的内部细节,对外形成一个边界(或者说形成一道屏障),只保留有限的对外接口使之与外部
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 考试试卷


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


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