實驗五-繼承與接口_第1頁
實驗五-繼承與接口_第2頁
實驗五-繼承與接口_第3頁
實驗五-繼承與接口_第4頁
實驗五-繼承與接口_第5頁
已閱讀5頁,還剩22頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、實驗五繼承與接口實驗目的1、掌握類的繼承細節(jié)內容,包括子類的繼承、子類對象創(chuàng)建、成員變量的繼承與隱藏、方法的繼承與重寫2、掌握重寫的目的以及怎樣使用super關鍵字3、掌握上轉型對象與接口回調技術4、掌握類怎樣實現(xiàn)接口,及面向接口的編程思想實驗內容1、根據(jù)附錄里的源代碼,按照注釋要求,完成代碼填空,使程序能夠運行得出結果。1)實驗1中國人與美國人2)實驗2銀行與利息3)實驗3面積之和4)實驗4歌手大賽5)實驗5天氣預報2、設計編寫程序完成以下任務。1)根據(jù)要求修改實驗2,參照建設銀行再編寫一個商業(yè)銀行(廣發(fā)行),讓程序輸出8000元在廣發(fā)行8年零212天的利息。2)根據(jù)要求修改實驗3,再增加一

2、種幾何圖形(梯形),并讓主類中的tuxing的某些元素是梯形的上轉型對象。3)仿照實驗5編寫一個程序實現(xiàn)模擬水杯中的水在不同溫度下可能出現(xiàn)的狀態(tài)。4)編寫一個接口并創(chuàng)建兩個實現(xiàn)該接口的類A、B。A、B類實現(xiàn)了接口的f方法,A類的f方法內容為計算1!+3!+5!+9!并返回結果,B類的f方法內容為計算2!+4!+6!+10!也返回結果;再編一個執(zhí)行類,執(zhí)行類運行時要求通過接口回調方式用同一個對象實例分別調用A類的f方法和B類的f方法。5)(1)定義一個汽車類Vehicle,要求如下:(知識點:類的繼承方法的覆蓋)(a)屬性包括:汽車品牌brand(String類型)、顏色color(String

3、類型)和速度speed(double類型)。(b)至少提供一個有參的構造方法(要求品牌和顏色可以初始化為任意值,但速度的初始值必須為0)。(c)為屬性提供訪問器方法。注意:汽車品牌一旦初始化之后不能修改。(d)定義一個一般方法run(),用打印語句描述汽車奔跑的功能。定義測試類VehicleTest,在其main方法中創(chuàng)建一個品牌為“benz”、顏色為“black”的汽車。(2)定義一個Vehicle類的子類轎車類Car,要求如下:(a)轎車有自己的屬性載人數(shù)loader(int類型)。(b)提供該類初始化屬性的構造方法。(c)重新定義run(),用打印語句描述轎車奔跑的功能。(d)定義測試類

4、Test,在其main方法中創(chuàng)建一個品牌為“Honda”、顏色為“red”,載人數(shù)為2人的轎車。面向對象基礎出過類似的,這個把繼承引入了6)Cola公司的雇員分為以下若干類:(知識點:多態(tài))ColaEmployee:這是所有員工總的父類,屬性:員工的姓名,員工的生日月份。方法:getSalary(intmonth)根據(jù)參數(shù)月份來確定工資,如果該月員工過生日,則公司會額外獎勵100元。SalariedEmployee:ColaEmployee的子類,拿固定工資的員工。屬性:月薪HourlyEmployee:ColaEmployee的子類,按小時拿工資的員工,每月工作超出160小時的部分按照1.5

5、倍工資發(fā)放。屬性:每小時的工資、每月工作的小時數(shù)SalesEmployee:ColaEmployee的子類,銷售人員,工資由月銷售額和提成率決定。屬性:月銷售額、提成率(5)定義一個類Company,在該類中寫一個方法,調用該方法可以打印出某月某個員工的工資數(shù)額,寫一個測試類TestCompany,在main方法,把若干各種類型的員工放在一個ColaEmployee數(shù)組里,并單元出數(shù)組中每個員工當月的工資。知識點:繼承與多態(tài)7)利用接口實現(xiàn)動態(tài)的創(chuàng)建對象:(知識點:接口)(1)創(chuàng)建4個類1蘋果2香蕉3葡萄4園丁(2)在三種水果的構造方法中打印一句話.以蘋果類為例classapplepublic

6、apple()System.out.println(“創(chuàng)建了一個蘋果類的對象”);(3)類圖如下:FruitPmrGardener+create():Fruit(4)要求從控制臺輸入一個字符串,根據(jù)字符串的值來判斷創(chuàng)建三種水果中哪個類的對象。運行結果如圖:FilsSd.itScEefaatarHavigate打3rdb電m=Jay-doc|lJeclaratimtestJivaApplicationC:IiHrtsr蘋果另痛了一個幸果的對象.實驗步驟實驗一、1實驗一、2Problems璘Javadoc良DechraftiomiSaveMoneyJavaApplicationE:MyEc呂的元看

7、在史金曼行呂正隼235五的*電24Z8.呂M仔仔0元的的五號在立逢汽行呂三筆235五的利奧二2%66,5600&x西館行私也tl舒7,7觸晅0元實驗一、3實驗一、4實驗一、5實驗二、1packageSFirst;publicclassBankintsavedMoney;intyear;doubleinterest;doubleinterestRate=0.29;publicdoublecomputerInterest()interest=year*interestRate*savedMoney;returninterest;publicvoidsetInterestRate(doublerat

8、e)interestRate=rate;)packageSFirst;publicclassGuangFaBankextendsBank(doubleyear;publicdoublecomputerlnterest()super.year=(int)year;doubler=year-(int)year;intday=(int)(r*1000);doubleyearInterest=puterInterest();/【代碼1】/super調用隱藏的computerInterest()方法doubledayInterest=day*0.0001*savedMoney;interest=year

9、Interest+dayInterest;returninterest;packageSFirst;publicclassSaveMoneypublicstaticvoidmain(Stringargs)intamount=8000;GuangFaBankbank1=newGuangFaBank();bank1.savedMoney=amount;bank1.year=8.212;bank1.setInterestRate(0.035);doubleinterest1=puterInterest();System.out.println(8000元在廣發(fā)行8年零212天的利息為:+intere

10、st1);實驗結果弒Problems:蘇Javadoc陶DeclarationcterminatedSaveMoney1JavaAppliestio記左廣及H目容隼212王第尼電拈:248.8實驗二、2packageSSecond;publicabstractclassGeometrypublicabstractdoublegetArea();)packageSSecond;publicclassMainClasspublicstaticvoidmain(Stringargs)Trapezoidtuxing=newTrapezoid(4.7,7.9,7);/有29個Geometry對象doub

11、les=tuxing.getArea();System.out.printf(梯形的面積為:s);)packageSSecond;publicclassTrapezoidextendsGeometry(doublea,b,h;Trapezoid(doublea,doubleb,doubleh)this.a=a;this.b=b;this.h=h;)/【代碼2】/重寫getArea()方法OverridepublicdoublegetArea()/TODOAuto-generatedmethodstubreturn(a+b)*h/2;)實驗結果Problenns璘JavadocMainChss弊

12、馬第面程出二44.1000實驗二、3packageSThird;publicclassBoiledStateimplementsWaterStateOverridepublicvoidshowState()/TODOAuto-generatedmethodstubSystem.out.println(開水。);/【代碼3】/重寫publicvoidshowState()方法packageSThird;publicclassIceStateimplementsWaterStatepublicvoidshowState()System.out.print(結冰。);packageSThird;pu

13、blicclassLittleWarmStateimplementsWaterState/【代碼1】/重寫publicvoidshowState()方法OverridepublicvoidshowState()/TODOAuto-generatedmethodstubSystem.out.println(微涼。);packageSThird;publicclassWarmStateimplementsWaterStateOverridepublicvoidshowState()/TODOAuto-generatedmethodstubSystem.out.println(溫水。);/【代碼2】

14、/重寫publicvoidshowState()方法packageSThird;publicclassWaterWaterStatestate;publicvoidshow()state.showState();publicvoidsetState(WaterStates)state=s;)packageSThird;publicclassWaterForecastpublicstaticvoidmain(Stringargs)WaterwaterState=newWater。;System.out.print(n溫度在90-100時水杯水的狀態(tài):);waterState.setState(n

15、ewBoiledState();waterState.show。;System.out.print(n溫度在50-80時水杯水的狀態(tài):);waterState.setState(newWarmState();waterState.show。;System.out.print(n溫度在30-50時水杯水的狀態(tài):);waterState.setState(newLittleWarmState();waterState.show。;System.out.print(n溫度在0度下時水杯水的狀態(tài):”);waterState.setState(newIceState();waterState.show。

16、;)packageSThird;publicinterfaceWaterStatepublicvoidshowState();)實驗結果且FProblems*Javadoc電DeclarationterminatedWaterForecastJavaApplicationE:MyEcl蔡豆在9好-1隨寸水杯的找在:開忒.之中及5。-呂64水樨水的根在:荒水.意宜空39-5團外水杯笊的壯耳,徵近.I施宜在有哎下才正朽水的就右:溶述,packageSForth;publicclassAimplementsobjectpublicintf(inta,intb)intsum=0,temp=1;for(

17、inti=a;i=b;i=i+2)for(intj=1;j=i;j+)temp*=j;sum+=temp;temp=1;returnsum;packageSForth;publicclassBimplementsobjectpublicintf(inta,intb)intsum=0,temp=1;for(inti=a;i=b;i=i+2)for(intj=1;jTest4JavaApplicationEEcliebinarycc1:H-3lH-5!.+9!的媽用=3680472l4-4:4-0!的洛原有:3669B6-6實驗五packageSFifth;publicclassCarextend

18、sVehicleintloader;publicCar(Stringbrand,Stringcolor,doublespeed,intl)super(brand,color,speed);this.loader=l;/TODOAuto-generatedconstructorstubpublicvoidrun()System.out.println(顏色為+super.getColor()+的+super.getBrand()+汽車的時速為:+super.getSpeed();packageSFifth;publicclassTestpublicstaticvoidmain(Stringarg

19、s)/TODOAuto-generatedmethodstubCarc=newCar(Honda,red”,300.0,20);c.run();packageSFifth;publicclassVehicleprivateStringbrand=BMW;/汽車品牌privateStringcolor=red;/汽車顏色privatedoublespeed=0;/汽車時速publicStringgetColor()returncolor;publicvoidsetColor(Stringcolor)this.color=color;publicdoublegetSpeed()returnspee

20、d;publicvoidsetSpeed(doublespeed)this.speed=speed;publicStringgetBrand()returnbrand;publicVehicle(Stringbrand,Stringcolor,doublespeed)/TODOAuto-generatedconstructorstubthis.brand=brand;this.color=color;this.speed=speed;publicvoidrun()System.out.println(brand+汽車的時速為:+speed);packageSFifth;publicclassV

21、ehicleTestpublicstaticvoidmain(Stringargs)/TODOAuto-generatedmethodstubVehiclev=newVehicle(benz,black,200.0);v.run();)實驗結果Problems濡Jg己dot篇DeH日VehicleTest1Javaben普工事的甘雙燈二2&9.0實驗六packageSSixth;publicclassColaEmployeeStringname;/員工姓名intmonth;/員工生日月份publicStringgetName()returnname;publicvoidsetName(Stri

22、ngname)=name;publicintgetMonth()returnmonth;publicvoidsetMonth(intmonth)this.month=month;intgetSalary(intmonth)if(month=this.month)return100;elsereturn0;packageSSixth;publicclassCompanyextendsColaEmployeevoidprint(Stringname,doublesalary,intmonth)StringName=name;doubleSalary=salary;intMonth=month;Sy

23、stem.out.println(員工+Name+在+Month+月+的薪資為:+Salary);packageSSixth;publicclassHourlyEmployeeextendsColaEmployeedoublehours;/每月工作的時間doublehsalary;/每小時的工資Companyc=newCompany();publicHourlyEmployee(Stringname,intmonth,doublehours,doublehsalary)/TODOAuto-generatedconstructorstubthis.hours=hours;this.hsalary

24、=hsalary;this.month=month;=name;publicdoublegetHours()returnhours;publicvoidsetHours(doublehours)this.hours=hours;publicdoublegetHsalary()returnhsalary;publicvoidsetHsalary(doublehsalary)this.hsalary=hsalary;publicintgetSalary(intmonth)if(this.hours=160)this.hsalary=this.hsalary*this.hours+super.get

25、Salary(month);c.print(name,hsalary,month);return0;elsethis.hsalary=(this.hours-160)*this.hsalary*1.5+160*this.hsalary+super.getSalary(month);c.print(name,hsalary,month);return0;packageSSixth;publicclassSalariedEmployeeextendsColaEmployeedoublesalary;/員工固定薪資(按月)Companyc=newCompany();publicSalariedEmp

26、loyee(Stringname,intmonth,doublesalary)/TODOAuto-generatedconstructorstub=name;this.month=month;this.salary=salary;publicintgetSalary(intmonth)this.salary=salary+super.getSalary(month);c.print(name,salary,month);return0;packageSSixth;publicclassSalesEmployeeextendsColaEmployeedoublemoney;/月銷售額double

27、ticheng;/提成率Companyc=newCompany();publicSalesEmployee(Stringname,intmonth,doublemoney,doubleticheng)/TODOAuto-generatedconstructorstubthis.money=money;this.month=month;=name;this.ticheng=ticheng;publicdoublegetMoney()returnmoney;publicvoidsetMoney(doublemoney)this.money=money;publicdoublegetTicheng(

28、)returnticheng;publicvoidsetTicheng(doubleticheng)this.ticheng=ticheng;publicintgetSalary(intmonth)this.money=money+super.getSalary(month)+(money*ticheng);c.print(name,money,month);return0;packageSSixth;publicclassSalesEmployeeextendsColaEmployeedoublemoney;/月銷售額doubleticheng;/提成率Companyc=newCompany

29、();publicSalesEmployee(Stringname,intmonth,doublemoney,doubleticheng)/TODOAuto-generatedconstructorstubthis.money=money;this.month=month;=name;this.ticheng=ticheng;publicdoublegetMoney()returnmoney;publicvoidsetMoney(doublemoney)this.money=money;publicdoublegetTicheng()returnticheng;)publicvoidsetTi

30、cheng(doubleticheng)this.ticheng=ticheng;)publicintgetSalary(intmonth)this.money=money+super.getSalary(month)+(money*ticheng);c.print(name,money,month);return0;)packageSSixth;publicclassTestcompanypublicstaticvoidmain(Stringargs)/TODOAuto-generatedmethodstubColaEmployeece=newColaEmployee3;ce0=newSal

31、ariedEmployee(aa”,9,3000.0);ce1=newHourlyEmployee(bb”,3,2000.0,180);ce2=newSalesEmployee(cc”,6,6000,0.3);ce0.getSalary(2);ce1.getSalary(2);ce2.getSalary(2);實驗結果保FProblems琮;Javadoc%Oeclara4ion用Console箕TestCo-mpany(1JJavaApplicationE:MyEclip&ebinf其Hmm在2,月的#資方:3際.0其ztbg?同為#矍后;5256-0.0戾hcu在2.目利手至臺;78d0.

32、0實驗七packageSSeventh;publicclassappleimplementsFruitpublicvoidapple()/TODOAuto-generatedmethodstubSystem.out.println(創(chuàng)建了一個蘋果類的對象);Overridepublicvoidbanana()/TODOAuto-generatedmethodstubOverridepublicvoidpear()/TODOAuto-generatedmethodstubpackageSSeventh;publicclassbananaimplementsFruitpublicvoidbanan

33、a()/TODOAuto-generatedmethodstubSystem.out.println(BHHW-);Overridepublicvoidapple()/TODOAuto-generatedmethodstubOverridepublicvoidpear()/TODOAuto-generatedmethodstubpackageSSeventh;publicinterfaceFruitvoidapple();voidbanana();voidpear();packageSSeventh;publicclasspearimplementsFruitpublicvoidpear()/

34、TODOAuto-generatedmethodstubSystem.out.println(創(chuàng)建了一個梨子類的對象);Overridepublicvoidapple()/TODOAuto-generatedmethodstubOverridepublicvoidbanana()/TODOAuto-generatedmethodstubpackageSSeventh;importjava.util.Scanner;publicclassTestpublicstaticvoidmain(Stringargs)/TODOAuto-generatedmethodstub/Fruitf=newbana

35、na();Scannerinput=newScanner(System.in);System.out.println(請輸入您要創(chuàng)建的類:”);Stringattr=input.nextLine();if(attr.equals(banana)Fruitfruit=newbanana();fruit.banana();elseif(attr.equals(apple)Fruitfruit=newapple();fruit.apple();elseif(attr.equals(pear)Fruitfruit=newpear();fruit.pear();)elseSystemut.println

36、(輸入有誤!);)實驗結果麻fProblems邙Javadoc時Determinateds-TestJavaApp濡鞋入宅要七春期莢:banana上建TT叁要走的走壇.評分標準A內容功能完善,編程風格好,人機接口界面好;B內容功能完善,編程風格良好,人機接口界面良好;C完成必做內容;D能完成必做內容;E未按時完成必做內容,或者抄襲(雷同者全部為E).參照書上實驗按模版要求,將【代碼】替換為Java程序代碼,編寫好完整的程序文檔,最后運行得到的相關文件,把實驗所得文件一起打包上交。(壓縮包的文件名為:學號后三位和名字開頭字母,如109zhh.RAR|ZIP)模板代碼附錄:實驗1中國人與美國人模板

37、代碼People.javapublicclassPeopleprotecteddoubleweight,height;publicvoidspeakHello()System.out.println(yayayaya);publicvoidaverageHeight()height=173;System.out.println(averageheight:+height);publicvoidaverageWeight()weight=70;System.out.println(averageweight:+weight);ChinaPeople.javapublicclassChinaPeo

38、pleextendsPeoplepublicvoidspeakHello()System.out.println(你好);publicvoidaverageHeight()height=168.78;System.out.println(中國人的平均身高:+height+厘米);/【代碼1】重寫publicvoidaverageWeight()方法,輸出:中國人的平均體重:65公斤publicvoidchinaGongfu()System.out.println,坐如鐘,站如松,睡如弓);AmericanPeople.javapublicclassAmericanPeopleextendsPe

39、ople/【代碼2】重寫publicvoidspeakHello()方法,輸出:Howdoyoudo/【代碼3】/重寫publicvoidaverageHeight()方法,輸出:Americansaverageheight:176cmpublicvoidaverageWeight()weight=75;System.out.println(Americansaverageweight:+weight+kg);publicvoidamericanBoxing()System.out.println(直拳、鉤拳、組合拳);BeijingPeople.javapublicclassBeijingP

40、eopleextendsChinaPeople/【代碼4】重寫publicvoidaverageHeight()方法,輸出:北京人的平均身高:172.5厘米/【代碼5】重寫publicvoidaverageWeight()方法,輸出:北京人的平均體重:70公斤publicvoidbeijingOpera()System.out.println(花臉、青衣、花旦和老生);Example.javapublicclassExamplepublicstaticvoidmain(Stringargs)ChinaPeoplechinaPeople=newChinaPeople();AmericanPeop

41、leamericanPeople=newAmericanPeople();BeijingPeoplebeijingPeople=newBeijingPeople();chinaPeople.speakHello();americanPeople.speakHello();beijingPeople.speakHello();chinaPeople.averageHeight();americanPeople.averageHeight();beijingPeople.averageHeight();chinaPeople.averageWeight();americanPeople.avera

42、geWeight();beijingPeople.averageWeight();chinaPeople.chinaGongfu();americanPeople.americanBoxing();beijingPeople.beijingOpera();beijingPeople.chinaGongfu();實驗2銀行與利息Bank.javapublicclassBankintsavedMoney;intyear;doubleinterest;doubleinterestRate=0.29;publicdoublecomputerInterest()interest=year*interes

43、tRate*savedMoney;returninterest;publicvoidsetInterestRate(doublerate)interestRate=rate;ConstructionBank.javapublicclassConstructionBankextendsBankdoubleyear;publicdoublecomputerInterest()super.year=(int)year;doubler=year-(int)year;intday=(int)(r*1000);doubleyearInterest=/【代碼1】/super調用隱藏的computerInte

44、rest()方法doubledayInterest=day*0.0001*savedMoney;interest=yearInterest+dayInterest;System.out.printf(%d元存在建設銀行%d年零d天的利息:%f元n,savedMoney,super.year,day,interest);returninterest;BankOfDalian.javapublicclassBankOfDalianextendsBankdoubleyear;publicdoublecomputerInterest()super.year=(int)year;doubler=year

45、-(int)year;intday=(int)(r*1000);doubleyearInterest=/【代碼2】/super調用隱藏的computerInterest()方法doubledayInterest=day*0.00012*savedMoney;interest=yearInterest+dayInterest;System.out.printf(%d元存在大連銀行%d年零%d天的利息:%f元n,savedMoney,super.year,day,interest);returninterest;SaveMoney.javapublicclassSaveMoneypublicsta

46、ticvoidmain(Stringargs)intamount=8000;ConstructionBankbank1=newConstructionBank();bank1.savedMoney=amount;bank1.year=8.236;bank1.setInterestRate(0.035);doubleinterest1=puterInterest();BankOfDalianbank2=newBankOfDalian();bank2.savedMoney=amount;bank2.year=8.236;bank2.setInterestRate(0.035);doubleinte

47、rest2=puterInterest();System.out.printf(兩個銀行利息相差元也”,血010512-血010511);實驗3面積之和模板代碼Geometry.javapublicabstractclassGeometrypublicabstractdoublegetArea();TotalArea.javapublicclassTotalAreaGeometrytuxing;doubletotalArea=0;publicvoidsetTuxing(Geometryt)tuxing=t;publicdoublecomputerTatalArea()/【代碼3】用循環(huán)語句讓t

48、uxing的元素調用getArea方法,并將返回的值累加到totalAreareturntotalArea;模板代碼模板代碼Rect.javapublicclassRectextendsGeometrydoublea,b;Rect(doublea,doubleb)this.a=a;this.b=b;/【代碼1】重寫getArea()方法Circle.javapublicclassCircleextendsGeometrydoubler;Circle(doubler)this.r=r;/【代碼2】重寫getArea()方法MainClass.javapublicclassMainClasspub

49、licstaticvoidmain(Stringargs)Geometry口tuxing=newGeometry29;/W29個Geometry對象for(inti=0;ituxing.length;i+)29個Geometry對象分成兩類if(i%2=0)tuxingi=newRect(16+i,68);elseif(i%2=1)tuxingi=newCircle(10+i);TotalAreacomputer=newTotalArea();computer.setTuxing(tuxing);System.out.printf(各種圖形的面積之和:n%f,puterTatalArea();

50、實驗4歌手大賽ComputerAverage.javapublicinterfaceComputerAveragepublicdoubleaverage(doublex);SongGame.javapublicclassSongGameimplementsComputerAveragepublicdoubleaverage(doublex)intcount=x.length;doubleaver=0,temp=0;for(inti=0;icount;i+)for(intj=0;jcount;j+)if(xjxi)temp=xj;xj=xi;xi=temp;for(inti=0;i2)aver=aver/(co

溫馨提示

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

最新文檔

評論

0/150

提交評論