实验四面向对象程序设计.doc

上传人:scccc 文档编号:12442711 上传时间:2021-12-03 格式:DOC 页数:23 大小:201.50KB
返回 下载 相关 举报
实验四面向对象程序设计.doc_第1页
第1页 / 共23页
实验四面向对象程序设计.doc_第2页
第2页 / 共23页
实验四面向对象程序设计.doc_第3页
第3页 / 共23页
实验四面向对象程序设计.doc_第4页
第4页 / 共23页
实验四面向对象程序设计.doc_第5页
第5页 / 共23页
点击查看更多>>
资源描述

《实验四面向对象程序设计.doc》由会员分享,可在线阅读,更多相关《实验四面向对象程序设计.doc(23页珍藏版)》请在三一文库上搜索。

1、实验四 面向对象程序设计实验类型: 验证性 实验课时: 8 指导教师: 陈志勇 时 间: 2015年 10月 21日课 次: 第 3、4节 教学周次: 第 7周 实验分室: 5-4 实验台号: 29 实 验 员: 马征 一、实验目的1.了解并掌握面向对象程序设计的基本思想和方法。2.掌握类的定义及对象的使用方法。3.掌握C#程序中的异常处理。4.理解程序中继承的使用。二、实验内容及要求1.定义长方形rectangle类:属性:长和宽两个属性;方法:无参数构造函数:长和宽的值为0两个参数构造函数:长和宽的值为对应参数值计算周长方法:返回长方形的周长计算面积方法:返回长方形的面积定义test类中,

2、在Main方法实例化两个对象,并输出其周长和面积程序:namespace shiyan4._1 class rectangle public int c, k, s, z; public rectangle() c = 0; k = 0; public rectangle(int c, int k) this.c = c; this.k = k; public int zhouchang(int c, int k) z = (c + k) * 2; return (z); public int mianji(int c, int k) s = c * k; return (s); class

3、test static void Main(string args) Console.WriteLine("请输入长方形的长:"); int x = Int32.Parse(Console.ReadLine(); Console.WriteLine("请输入长方形的宽:"); int y= Int32.Parse(Console.ReadLine(); rectangle a = new rectangle(x, y); Console.WriteLine("长方形的周长为:0", a.zhouchang(x, y); Console

4、.WriteLine("长方形的面积为:0", a.mianji(x, y); Console.Read(); 运行结果: 2(1)设计一个Person1类,包含下列数据:字段:姓名(name)、血型(blood)、体重(weight)、身高(height)。属性:Name和Blood访问姓名(name)、血型(blood)字段方法:显示姓名PrintName()、显示血型PrintBlood()、显示重量PrintWeight()、显示身高PrintHeight()、增加身高AddHeight()、增加体重AddWeight()、减少体重SubWeight()、显示对象本

5、身Tostring()。构造函数:Person1() Person1(string name,string blood,int weight,int height) 对于姓名的设置要进行验证,字符数不能大于4 ,不能小于2。(2)设计一个Person2类,除了Person1类字段及方法外,增加字段电话telephone,增加方法PrintTelephone(),两个构造函数person2()和Person2(string name,string blood,int weight,int height,int telephone)(3)在主函数中创建Person1类和Person2类的对象,进行

6、测试。程序:namespace shiyan4._2 class person1 public string name, blood; public int weight, height; public person1() name = "" blood = "" weight = 0; height = 0; public person1(string name, string blood, int weight, int height) name = "张三" blood = "AB" weight = 68;

7、 height = 180; public string Name get return name; public string Blood get return blood; public int Weight get return weight; set weight = value; public int Height get return height; set weight = value; public string printname() return name; public string printblood() return blood; public int printw

8、eight() return weight; public int printheight() return height; public int Addweight(int x) return weight + x; public int Subweight(int x) return weight - x; public int Addheight(int x) return height + x; public int Subheight(int x) return height - x; class person2 public string name, blood; public i

9、nt weight, height, telephone; public person2() name = "" blood = "" weight = 0; height = 0; telephone = 0; public person2(string name, string blood, int weight, int height, int telephone) name = "张三" blood = "AB" weight = 68; height = 180; telephone = 56782434

10、; public string Name get return name; public string Blood get return blood; public int Weight get return weight; set weight = value; public int Height get return height; set weight = value; public string Printname() return name; public string Printblood() return blood; public int Printweight() retur

11、n weight; public int Telephone() return telephone; public int Printheight() return height; public int Addweight(int x) return weight + x; public int Subweight(int x) return weight - x; public int Addheight(int x) return height + x; public int Subheight(int x) return height - x; class test static voi

12、d Main(string args) string name = "张三" string blood = "AB" int weight = 68; int height = 180; person1 m = new person1(); Console.WriteLine("姓名:0", name); Console.WriteLine("血型:0", blood); Console.WriteLine("体重:0", weight); Console.WriteLine("身高:

13、0", height); Console.WriteLine("请输入要增加的身高"); int p = Int32.Parse(Console.ReadLine(); int h=m.Addheight(p); Console.WriteLine("身高为:0", height + h); Console.WriteLine("请输入要增加的体重"); int t = Int32.Parse(Console.ReadLine(); int z = m.Addheight(t); Console.WriteLine(&quo

14、t;体重为:0", weight + z); Console.WriteLine("请输入要减少的体重"); int j = Int32.Parse(Console.ReadLine(); int n = m.Addheight(j); Console.WriteLine("体重为:0", weight + z - n); Console.Read(); 运行结果:3. 输入以下程序,体会错误处理机制:class Program static void Main(string args) Console.WriteLine("请输入年

15、龄"); int i = int.Parse(Console.ReadLine(); 输入年龄时分别输入”aa”、”1111111111111111111111”看看程序运行会发生什么,注意观察系统信息。输入aa,提示输入格式不正确输入111111111111111111,提示输入的值太大或太小然后运行以下程序,分别输入120、aa、1111111111111111111看看这回程序运行状态。class AgeException : Exception public string reason; public AgeException(int age) if (age < 0)

16、 reason = "太小了" else if (age > 110) reason = "太大了" class Program static void Main(string args) try Console.WriteLine("请输入年龄"); int i = int.Parse(Console.ReadLine(); if (i < 0 | i > 110) throw new AgeException(i); catch (AgeException e) Console.WriteLine(e.reaso

17、n); catch (FormatException e) Console.WriteLine(e.Message); catch (Exception e) Console.WriteLine(e.Message); (1)为什么AgeException的父类定义为Exception,不定义行吗?Exception 是异常类的基类,要继承父类的属性及方法,必须定义。(2)在这个程序中哪个是系统抛出的异常,哪个是代码自己抛出的?AgeException是代码自己抛出的,FormateException是系统抛出的。4. 类的层次关系如图所示:PointCircleRectangleCylind

18、er(1) 点(Point)类具有以下属性:坐标X、坐标Y,且具有以下方法: ToString( ):点的字符串表示形式(2)圆(Circle)类具有以下属性:坐标X、坐标Y、半径Radius,且具有以下方法: Diameter():求直径 CircumFerence():求周长 Area():求圆的面积 ToString():圆的字符串表示形式(3)矩形(Rectangle)类具有以下属性:坐标X、坐标Y、长length、宽width,且具有以下方法: zhouchang():求周长 Area():求矩形的面积 ToString():矩形的字符串表示形式(4)除继承了Circle类的属性和方

19、法外,还应具有:Height:圆柱体高度,Volumn(): 求体积方法:底面积*高,并重载:Area():求圆柱体表面积:2*底面积+底周长*高(5)编程实现上述类,并定义Test类进行测试。程序:namespace shiyan4 class Point protected double x, y; public Point() x = 0.0; y = 0.0; public Point(double x, double y) this.x = x; this.y = y; public override string ToString() return "点的横坐标为:&qu

20、ot; + x + ",纵坐标为:" + y ; public virtual double CircumFerence() return 0.0; public virtual double Area() return 0.0; class Cirle : Point protected double Radius; protected double PI = 3.1415926; protected double diameter, circumFerence, area; public Cirle() x = 0.0; y = 0.0; Radius = 0.0; p

21、ublic Cirle(double Radius) this.Radius = Radius; public double Diameter(double Radius) diameter = this.Radius * 2; return diameter; public override double CircumFerence() circumFerence = 2 * PI * this.Radius; return circumFerence; public virtual double Area(double r) area = PI * r * r; return area;

22、public override string ToString() return "圆的半径为:" + this.Radius + "n" + "圆的直径为:" + this.diameter + "n" + "圆的周长为:" + this.circumFerence + "n" + "圆的面积为:" + this.area; class Rectangle : Point protected double length, width, s, c; pub

23、lic Rectangle() x = 0.0; y = 0.0; length = 0.0; width = 0.0; public Rectangle(double length, double width) length = this.length; width = this.width; public double zhouchang(double width, double length) c = 2 * (length + width); return c; public virtual double Area(double width, double length) s = wi

24、dth * length; return s; public override string ToString() return "矩形的周长为:" + c + "n" + "矩形的面积为:" + s; class Cylinder : Cirle public double height, volumn, mj; public Cylinder() ; public Cylinder(double Radius, double height) : base(Radius) this.height = height; public d

25、ouble Volumn(double Radius, double height) volumn = PI * Radius * Radius * height; return volumn; public override double Area() mj = 2 * PI * Radius * Radius + 2 * PI * Radius * height; return mj; public override string ToString() return "圆柱体的体积为:" + volumn + "n" + "圆柱体的面积为:

26、" + mj; class Text static void Main(string args) double x, y, Radius, zj, zc, mj, jzc, jmj, length, width, height, volumn, zmj; Console.Write("请输入横坐标x:"); x = double.Parse(Console.ReadLine(); Console.Write("请输入纵坐标y:"); y = double.Parse(Console.ReadLine(); Point p = new Point

27、(x, y); Console.WriteLine(p); Console.Write("请输入圆的半径:"); Radius = double.Parse(Console.ReadLine(); Point a = new Point(); Point b = new Point(x, y); Cirle c = new Cirle(Radius); zj = c.Diameter(Radius); Console.WriteLine("该圆的直径为:" + zj); zc = c.CircumFerence(); Console.WriteLine(

28、"该圆的周长为:" + zc); mj = c.Area(Radius); Console.WriteLine("该圆的面积为:" + mj); Console.WriteLine(c); Console.Write("请输入矩形的宽:"); width = int.Parse(Console.ReadLine(); Console.Write("请输入矩形的长:"); length = int.Parse(Console.ReadLine(); Rectangle z = new Rectangle(width,

29、 length); jzc = z.zhouchang(width, length); jmj = z.Area(width, length); Console.WriteLine(z); Console.Write("请输入圆柱的高:"); height = int.Parse(Console.ReadLine(); Cylinder cirle = new Cylinder(Radius, height); volumn = cirle.Volumn(Radius, height); zmj = cirle.Area(); Console.WriteLine(cirle

30、); Console.ReadLine(); 运行结果:5.定义一个学生类,包含学号、姓名、性别等信息,定义一个班级类,在班级类中包含一个可以指定个数的学生类数组,并提供两个索引器,一个是int型参数用于向数组指定下标元素赋值或读取操作,一个是String型参数,用于根据参数在数组中查找指定元素。然后编写一个测试程序进行验证。程序:namespace shiyan4 class student public string name; public string xingbie; public string xuehao; public student() name = "张三&quo

31、t; xingbie = "男" xuehao = "0914130528" public student(string name, string xingbie, string xuehao) this.name = name; this.xingbie = xingbie; this.xuehao = xuehao; class banji public student number = new studentsize; static int size = 26; public student thisint index get return num

32、berindex; set numberindex = value; public student thisstring s get int i; for (i = 0; i < 26; i+) if (numberi.name = s) return numberi; return null; class test static void Main(string args) student a = new student(); student b = new student("李四", "男", "0914130529"); student c = new student("王丽", "女", "0914130530"); student d = new student("周伟", "男", "0914130531"); student f = new student("刘丽", "女", "0914130532"); student e = new s

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1