資源描述:
《JAVA 繼承和多態(tài)實驗報告》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、實驗報告實驗項目名稱:繼承和多態(tài)(所屬課程:Java語言程序設(shè)計)院系:專業(yè)班級:姓名:學(xué)號:實驗地點:指導(dǎo)老師:本實驗項目成績:教師簽字:日期:1.實驗?zāi)康模?)掌握類的繼承機制。(2)熟悉類中成員變量和方法的訪問控制。(3)熟悉方法或構(gòu)造方法多態(tài)性。2.實驗內(nèi)容(1)模擬編寫程序,理解類的繼承、多態(tài)、繼承和多態(tài)規(guī)則。(2)獨立編程,實現(xiàn)類的繼承和多態(tài)。3.實驗作業(yè)設(shè)計一個類Shape(圖形)包含求面積和周長的area()方法和perimeter()方法以及設(shè)置顏色的方法SetColor(),并利用Java多態(tài)技術(shù)設(shè)計其子類Cir
2、cle(圓形)類、Rectangle(矩形)類和Triangle(三角形)類,并分別實現(xiàn)相應(yīng)的求面積和求周長的方法。每個類都要覆蓋toString方法。海倫公式:三角形的面積等于s(s-a)(s-b)(s-c)的開方,其中s=(a+b+c)/2程序代碼為:Class包packageClass;publicclassShape{privateStringcolor="while";publicShape(Stringcolor){this.color=color;}publicvoidsetColor(Stringcolor){thi
3、s.color=color;}publicStringgetColor(){7實驗報告returncolor;}publicdoublegetArea(){return0;}publicdoublegetPerimeter(){return0;}publicStringtoString(){return"color:"+color;}}packageClass;publicclassCircleextendsShape{privatedoubleradius;publicCircle(Stringcolor,doubleradius
4、){super(color);this.radius=radius;}publicvoidsetRadius(doubleradius){this.radius=radius;}publicdoublegetRadius(){returnradius;}publicdoublegetCircleArea(){return3.14*radius*radius;}publicdoublegetCirclePerimeter(){return3.14*2*radius;}7實驗報告publicStringtoString(){return
5、"TheAreais:"+getCircleArea()+"ThePerimeteris:"+getCirclePerimeter();}}packageClass;publicclassRectangleextendsShape{privatedoublewidth;privatedoubleheight;publicRectangle(Stringcolor,doublewidth,doubleheight){super(color);this.width=width;this.height=height;}publicvo
6、idsetWidth(doublewidth){this.width=width;}publicdoublegetWidth(){returnwidth;}publicvoidsetHeight(doubleheight){this.height=height;}publicdoublegetHeight(){returnheight;}publicdoublegetRectangleArea(){returnwidth*height;}publicdoublegetRectanglePerimeter(){return2*(wid
7、th+height);}7實驗報告publicStringtoString(){return"TheAreais:"+getRectangleArea()+"ThePerimeteris:"+getRectanglePerimeter();}}packageClass;publicclassTriangleextendsShape{privatedoublea;privatedoubleb;privatedoublec;privatedoubles;publicTriangle(Stringcolor,doublea,doubl
8、eb,doublec,doubles){super(color);this.a=a;this.b=b;this.c=c;this.s=s;}publicvoidsetA(doublea){this.a=a;}publicdoubleg