JAVA繼承和多態(tài)實(shí)驗(yàn)報(bào)告_第1頁
JAVA繼承和多態(tài)實(shí)驗(yàn)報(bào)告_第2頁
JAVA繼承和多態(tài)實(shí)驗(yàn)報(bào)告_第3頁
JAVA繼承和多態(tài)實(shí)驗(yàn)報(bào)告_第4頁
JAVA繼承和多態(tài)實(shí)驗(yàn)報(bào)告_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、實(shí)驗(yàn)項(xiàng)目名稱繼承和多態(tài)院系:學(xué)號:姓名: 指導(dǎo)老師:所屬課程:Java語言程序設(shè)計(jì)專業(yè)班級:實(shí)驗(yàn)地點(diǎn):本實(shí)驗(yàn)項(xiàng)目成績:教師簽字:日期:1實(shí)驗(yàn)?zāi)康?1)掌握類的繼承機(jī)制。(2)熟悉類中成員變量和方法的訪問控制。(3 )熟悉方法或構(gòu)造方法多態(tài)性。2.實(shí)驗(yàn)內(nèi)容(1)模擬編寫程序,理解類的繼承、多態(tài)、繼承和多態(tài)規(guī)則。(2 )獨(dú)立編程,實(shí)現(xiàn)類的繼承和多態(tài)。3.實(shí)驗(yàn)作業(yè)設(shè)計(jì)一個(gè)類Shape (圖形)包含求面積和周長的area()方法和perimeter()方法以及設(shè)置顏色的方法 SetColor(),并利用Java多態(tài)技術(shù)設(shè)計(jì)其子類Circle (圓形)類、Rectangle(矩形)類和 Tria ngl

2、e (三角形)類,并分別實(shí)現(xiàn)相應(yīng)的求面積和求周長的方法。每個(gè)類都要覆蓋toString 方法。海倫公式:三角形的面積等于s(s-a)(s-b)(s-c) 的開方,其中s=(a+b+c)/2程序代碼為:Class 包P ackageClass;publicp rivateclass Shape Stri ngcolor"while"publicthisShap e(Stri ng color).color = color;publicthisvoid setColor(Stri ng color).color = color;publicreturnStri ng getCo

3、lor() color ;p ublic doublegetArea()return 0;p ublicdoubleget PerimeterOreturn 0;public Stri ng toStri ng()return "color:" + colorClass;packagepublicp rivatedouble radiusclass Circleexte ndsShape p ublicCircle(Stri ng color,super (color);double radius) this.radius = radius;publicthisvoid s

4、etRadius( double radius) .radius = radius;publicreturndouble getRadius()radius ;p ublicdouble getCircleArea()return3.14* radius * radiusp ublicdoublegetCircle PerimeterOreturn 3.14*2* radius ;public Stri ng toStri ng()retur n "The Area is:"+ getCircleAreaO+ "n The P erimeter is:"

5、+ getCircle PerimeterO;p ackageClass;p ublicclass Recta ngleprivatedouble width ;privatedouble height ;publicRecta ngle(Stri ng color,double width,double height) super (color);this.width = width;this.height = height;publicvoid setWidth(doublewidth)this.width = width;publicdouble getWidth()returnwidt

6、h ;publicvoid setHeight(doubleheight)this.height = height;publicdouble getHeight()Sha peexte ndsretur nheightp ublicdoublegetRecta ngleAreaOreturn width * height ;p ublicdoublegetRecta ngle Perimeter()return 2*( width + height );public Stri ng toStri ng()+ getRecta ngleAreaOretur n "The Area is

7、:"+ "n The P erimeter is:"+ getRecta ngle Perimeter。;p ackageClass;p ublicclassTria ngleexte ndsShapeprivatedoublea;privatedoubleb;privatedoublec;privatedoubles;publicTria ngle(Stringdouble a, double b, double c, double s)super (color); this=a;thisthisthispublicthis=b;=c;=s;void setA(

8、 double.a = a;publicreturn a;double getA()a)publicvoid setB( doubleb)this.b = b;publicreturndouble getB() b;publicthisvoid setC( doublec).c = c;p ublicdouble getC()return c;p ublicdoublegetTria ngleAreaOreturn Math. sqrt ( s*( s- a)*( s- b)*( s- c);p ublicdoublegetTria ngle Penmeter()public Stri ng

9、toStri ng()retur n "The Area is:"return a + b + c;+ getTria ngleAreaO+ "nThe P erimeter is:"+ getTria ngle Perimeter。;p ublicstatic voidScanner input =System. out .print(main( Stri ng args) new Sca nn er(System."請輸入圓的半徑:in );“);double radius = inp ut. nextDouble();Circle cir

10、cle =new Circle( n ullout .println(circle.toString();System.,radius);System.doubleSystem.doubleout .print("n請輸入矩形的寬:width = inp ut .n extDouble();out .print("請輸入矩形的高:height = inp ut .n extDouble();“);“);Main 包packageMain;imp ortClass.Sha pe;imp ortClass.Circle;imp ortClass.Recta ngle;imp o

11、rtClass.Tria ngle;imp ortjava.util.Sca nner;publicclass test Recta ngle recta ngle =new Recta ngle( n ull , width, height);System. out .println(rectangle.toString();System. out .print( "n請輸入三角形的第一條邊a : ”);doublea = inp ut. nextDoubleO;System.doubleSystem.doubledoubleout .print("請輸入三角形的第二條邊b = inp ut. nextDoubleO;out .print("請輸入三角形的第三條邊c = inp ut. nextDoubleO;s = (a + b + c)/2;b : “);c : “);,a, b, c, s);Tria ngle tria ngle =new Trian gle(n ullSystem. out .println(tnangle.toStringO);運(yùn)行結(jié)果為:諳輸入矩形的寬;2請輸入矩形的高!

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論