资源描述:
《继承与多态实验报告.doc》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、实验五继承与多态专业班级学号姓名实验学时2实验类型验证性实验地点软件工程实验室实验时间2013-11-21指导老师曹记东实验成绩年月日一、实验目的:理解继承与多态的概念,掌握三种访问控制的区别,掌握抽象类、接口与包的应用。二、实验内容:1.参考教材P112,示例程序C5_17,掌握抽象类的应用。2.参考教材P116,示例程序C5_19,掌握接口的使用方法。3.参考教材P121,示例程序C5_20,掌握包的使用方法。三、实验要求:1.完成示例程序的验证。2.写出实验报告。四、实验步骤与结果:(由学生按照实验内容完成)1.示例程序c5_17importjava.awt.*;importjav
2、a.applet.*;abstractclassShapes//定义一个抽象类Shapes{publicintx,y;//x、y为画图的坐标publicintwidth,height;publicShapes(intx,inty,intwidth,intheight){this.x=x;this.y=y;this.width=width;this.height=height;}abstractdoublegetArea();//求图形面积的抽象方法abstractdoublegetPerimeter();//求图形周长的抽象方法}classSquareextendsShapes//由抽象
3、类Shapes派生的子类——矩形类{publicdoublegetArea(){return(width*height);}publicdoublegetPerimeter(){return(2*width+2*height);}publicSquare(intx,inty,intwidth,intheight){super(x,y,width,height);}}classTriangleextendsShapes//由抽象类Shapes派生的子类——三角形类{publicdoublec;//斜边publicdoublegetArea(){return(0.5*width*height
4、);}publicdoublegetPerimeter(){return(width+height+c);}publicTriangle(intx,inty,intbase,intheight){super(x,y,base,height);c=Math.sqrt(width*width+height*height);}}classCircleextendsShapes//由抽象类Shapes派生的子类——圆类{publicdoubler;//半径publicdoublegetArea(){return(r*r*Math.PI);}publicdoublegetPerimeter(){r
5、eturn(2*Math.PI*r);}publicCircle(intx,inty,intwidth,intheight){super(x,y,width,height);r=(double)width/2.0;}}publicclassc5_18extendsApplet{/****/privatestaticfinallongserialVersionUID=-5589238944558587862L;SquareBox=newSquare(5,15,25,25);Triangletri=newTriangle(5,50,8,4);CircleOval=newCircle(5,90
6、,25,25);publicvoidpaint(Graphicsg){//画正方形g.drawRect(Box.x,Box.y,Box.width,Box.height);g.drawString("BoxArea:"+Box.getArea(),50,35);g.drawString("BoxPerimeter:"+Box.getPerimeter(),50,55);g.drawString("triArea:"+tri.getArea(),50,75);g.drawString("triPerimeter:"+tri.getPerimeter(),50,95);g.drawOval(
7、Oval.x,Oval.y,Oval.width,Oval.height);//画圆g.drawString("ovalArea:"+Oval.getArea(),50,115);}}2.示例程序C5_19importjava.awt.*;importjava.applet.*;interfaceShapes{abstractdoublegetArea();abstractdoublegetPerimeter();}classCoo