資源描述:
《繼承與多態(tài)實驗報告.doc》由會員上傳分享,免費在線閱讀,更多相關內(nèi)容在應用文檔-天天文庫。
1、實驗五繼承與多態(tài)專業(yè)班級學號姓名實驗學時2實驗類型驗證性實驗地點軟件工程實驗室實驗時間2013-11-21指導老師曹記東實驗成績年月日一、實驗目的:理解繼承與多態(tài)的概念,掌握三種訪問控制的區(qū)別,掌握抽象類、接口與包的應用。二、實驗內(nèi)容:1.參考教材P112,示例程序C5_17,掌握抽象類的應用。2.參考教材P116,示例程序C5_19,掌握接口的使用方法。3.參考教材P121,示例程序C5_20,掌握包的使用方法。三、實驗要求:1.完成示例程序的驗證。2.寫出實驗報告。四、實驗步驟與結果:(由學生按照實驗內(nèi)容完成)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