




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第八次實(shí)驗(yàn)實(shí)驗(yàn)1:中國(guó)人、北京人和美國(guó)人實(shí)驗(yàn)要求:編寫程序模擬中國(guó)人、美國(guó)人是人,北京人是中國(guó)人。除主類外,程序中還有4個(gè)類:People、ChinaPeople、AmericanPeople和BeijingPeople類。要求如下:People類有權(quán)限是protected的double型成員變量height和weight,以及publicvoidspeakHello()、publicvoid averageHeight()和publicvoidaverageWeight()方法。ChinaPeople類是People的子類,新增了publicvoidaverageHeight()和publicvoidaverageWeight()方法。AmericanPeople類是People的子類,新增方法publicvoidAmericanBoxing() 。要求AmericanPeople重寫父類的publicvoidspeakHello()、publicvoidaverageHeight()和publicvoidaverageWeight()方法。BeijingPeople類是ChinaPeople的子類,新增publicvoidbeijingOpera()方法。實(shí)驗(yàn)代碼://People.javapublicclassPeople{protecteddoubleweight,height;publicvoidspeakHello(){System.out.println("yayayaya");}publicvoidaverageHeight(){height=173;System.out.println("averageheight:"+height);}publicvoidaverageWeight(){weight=70;System.out.println("averageweight:"+weight);}}//ChinaPeople.javapublicclassChinaPeopleextendsPeople{publicvoidspeakHello(){System.out.println("您好");}publicvoidaverageHeight(){height=168.78;System.out.println("中國(guó)人的平均身髙:"+height+"厘米");}publicvoidaverageWeight(){weight=65;System.out.printin(”中國(guó)人的平均體重:"+weight+"千克");}publicvoidchinaGongfu(){System.out.println("坐如鐘,站如松,睡如弓");}}//AmericanPeople.javapublicclassAmericanPeopleextendsPeople{publicvoidspeakHello(){System.out.println("Howdoyoudo");}publicvoidaverageHeight(){height=176;System.out.println("American'saverageheight:"+height+"厘米");}publicvoidaverageWeight(){weight=75;System.out.println("American'saverageweight:"+weight+"kg");}publicvoidamericanBoxing(){System.out.println("直拳,勾拳,組合拳");}}//BeijingPeople.javapublicclassBeijingPeopleextendsChinaPeople{publicvoidaverageHeight(){height=172.5;System.out.println(”北京人的平均身髙:"+height+"厘米");}publicvoidaverageWeight(){weight=70;System.out.printin(”北京人得平均體重:"+weight+"千克");}pubiicvoidbeijingOpera(){System.out.printin(”花臉、青衣、花旦和老生");}}//Exampie.javapubiicciassExampie{pubiicstaticvoidmain(Stringarg[]){ChinaPeoplechinaPeople=newChinaPeople();AmericanPeopleamericanPeople=newAmericanPeople();BeijingPeoplebeijingPeople=newBeijingPeople();chinaPeople.speakHello();americanPeople.speakHello();beijingPeople.speakHello();chinaPeople.averageHeight();americanPeople.averageHeight();beijingPeople.averageHeight();chinaPeople.averageWeight();americanPeople.averageWeight();beijingPeople.averageWeight();chinaPeople.chinaGongfu();americanPeople.americanBoxing();beijingPeople.beijingOpera();beijingPeople.chinaGongfu();}}4.實(shí)驗(yàn)分析:方法重寫時(shí)要保證方法的名字、類型、參數(shù)的個(gè)數(shù)和類型同父類的某個(gè)方法完全想同。這樣,子類繼承的方法才能被隱藏。子類在重寫方法時(shí),如果重寫的方法是static方法,static關(guān)鍵字必須保留;如果重寫的方法是實(shí)例方法,重寫時(shí)不可以用static修飾。如果子類可以繼承父類的方法,子類就有權(quán)利重寫這個(gè)方法,子類通過(guò)重寫父類的方法可以改變父類的具遺體行為。5.實(shí)驗(yàn)后的練習(xí):People類中的publicvoidspeakHello()publicvoidaverageHeight()publicvoidaverageWeight()三個(gè)方法的方法體中的語(yǔ)句是否可以省略答:可以省略,因?yàn)槭÷院蠼Y(jié)果沒有變化實(shí)驗(yàn)2:銀行計(jì)算利息實(shí)驗(yàn)要求:假設(shè)銀行bank已經(jīng)有了按整年year計(jì)算利息的一般方法,其中year只能取正整數(shù)。比如,按整年計(jì)算的方法:DoublecomputerInternet(){Interest=year*0.35*saveMoney;Returninterest;}建設(shè)銀行constructionBank是bankde子類,準(zhǔn)備隱藏繼承的成員變量year,并重寫計(jì)算利息的方法,即自己聲明一個(gè)double型的year變量。要求construetionbank和bankofDalian類是bank類的子類,constructionbank和bankofdalian都使用super調(diào)用隱藏的按整年計(jì)算利息的方法。實(shí)驗(yàn)代碼://Bank.javapublicclassBank{intsavedMoney;intyear;doubleinterest;doubleinterestRate=0.29;publicdoublecomputerInterest(){interest=year*interestRate*savedMoney;returninterest;}publicvoidsetInterestRate(doublerate){interestRate=rate;}}//ConstructionBank.javapublicclassConstructionBankextendsBank{doubleyear;publicdoublecomputerInterest(){super.year=(int)year;doubler=year-(int)year;intday=(int)(r*1000);doubleyearInterest=puterInterest();doubledayInterest=day*0.0001*savedMoney;interest=yearInterest+dayInterest;System.out.printf("%d元存在建設(shè)銀行%d年零%d天的利息:%f元\n",savedMoney,super.year,day,interest);returninterest;}}//BankOfDalian.javapublicclassBankOfDalianextendsBank{doubleyear;publicdoublecomputerInterest(){super.year=(int)year;doubler=year-(int)year;intday=(int)(r*1000);doubleyearInterest=puterInterest();doubledayInterest=day*0.00012*savedMoney;interest=yearInterest+dayInterest;System.out.printf("%d元存在大連銀行%d年零%d天的利息:%f元\n",savedMoney,super.year,day,interest);returninterest;}//SaveMoney.javapublicclassSaveMoney{publicstaticvoidmain(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);doubleinterest2=puterInterest();System.out.printf("兩個(gè)銀行利息相差%f元\n",interest2-interestl);}}實(shí)驗(yàn)結(jié)果:山C:\PROGRA"1\IINOKS"1\JCKEAT"1\GE2001.exepQBB工存在建設(shè)銀抵E年勢(shì)盹天拘禾息:242取陰00胸元P000丸存在夭逹銀行8^^236天的未I、息:24t6.56&BQ0無(wú)P個(gè)4艮廳剎息相差3?.760086元Pressanj;kei?tocontinue■?■.實(shí)驗(yàn)分析:(1) 子類不繼承父類的構(gòu)造方法,因此子類在其構(gòu)造方法中需使用super來(lái)調(diào)用父類的構(gòu)造方法,并且super必須是子類構(gòu)造方法中的頭一條語(yǔ)句。(2) 當(dāng)super調(diào)用被隱藏的方法時(shí),該方法中出現(xiàn)的成員變量是被子類隱藏的成員變量或繼承的成員變量。實(shí)驗(yàn)后的練習(xí):參照建設(shè)銀行或大連銀行,在編寫一個(gè)商業(yè)銀行,讓程序輸出8000元存在商業(yè)銀行8年零236天的利息。//Bank.javapublicclassBank{intsavedMoney;intyear;doubleinterest;doubleinterestRate=0.29;publicdoublecomputerInterest(){interest=year*interestRate*savedMoney;returninterest;}publicvoidsetInterestRate(doublerate){interestRate=rate;}}//CommercialBankpublicclassCommercialBankextendsBank{doubleyear;publicdoublecomputerInterest(){super.year=(int)year;doubler=year-(int)year;intday=(int)(r*1000);doubleyearInterest=puterInterest();doubledayInterest=day*0.00012*savedMoney;interest=yearInterest+dayInterest;System.out.printf("%d元存在商業(yè)銀行%d年零%d天的利息:%f元\n",savedMoney,super.year,day,interest);returninterest;}}//SaveMoney.javapublicclassSaveMoney{publicstaticvoidmain(Stringargs[]){intamount=8000;CommercialBankbank=newCommercialBank();bank.savedMoney=amount;bank.year=8.236;bank.setInterestRate(0.035);doubleinterest=puterInterest();}
22C:\PROGEL4"1\IINOKS"1\JCREAT"1\GE2001.ejluBB$000兀存在商業(yè)銀行8年零2并天:2466.5600007EPi*essanjkeytocnmlzinuE 』實(shí)驗(yàn)3:公司支出的總薪水實(shí)驗(yàn)要求:要求有一個(gè)abstract類,類名為Employee。Employee的子類有YearWorker、MonthWorker、WeekWorker。YearWorker對(duì)象按年領(lǐng)取薪水,MonthWorker按月領(lǐng)取薪水、WeekWorker按周領(lǐng)取的薪水。Employee類有一個(gè)abstract方法:publicabstractearnings();子類必須重寫父類的earings()方法,給出各自領(lǐng)取報(bào)酬的具體方式。有一個(gè)Company類,該類用Employee對(duì)象數(shù)組作為成員,Employee對(duì)象數(shù)組的單元可以是YearWorker對(duì)象的上轉(zhuǎn)型對(duì)象、MonthWorker獨(dú)享的上轉(zhuǎn)型獨(dú)享或weekworker對(duì)象的上轉(zhuǎn)型獨(dú)享。程序能輸出Company對(duì)象一年需要支付的薪水總額。實(shí)驗(yàn)代碼:abstractclassEmployee{publicabstractdoubleearnings();}classYearWorkerextendsEmployee{publicdoubleearnings(){return12000;}}classMonthWorkerextendsEmployee{publicdoubleearnings(){return12*2300;}classWeekWorkerextendsEmployee{publicdoubleearnings(){return52*780;}}classCompany{Employee[]employee;doublesalaries=0;Company(Employee[]employee){this.employee=employee;}publicdoublesalariesPay(){salaries=0;for(inti=0;i<employee.length;i++){salaries=salaries+employee[i].earnings();}returnsalaries;}}publicclassCompanySalary{publicstaticvoidmain(Stringargs[]){Employee[]employee=newEmployee[29];for(int
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 西藏清代糧倉(cāng)管理辦法
- 自貢養(yǎng)殖大棚管理辦法
- 逆向思維訓(xùn)練培訓(xùn)課件
- 肝膽微課護(hù)理課件
- 肝病護(hù)理課件
- 甘肅高考模擬數(shù)學(xué)試卷
- 肝病中醫(yī)護(hù)理課件
- 福建省泉州初三數(shù)學(xué)試卷
- 東營(yíng)三模數(shù)學(xué)試卷
- 課件制作的培訓(xùn)總結(jié)
- TCRHA 088-2024 病理免疫組織化學(xué)檢測(cè)質(zhì)控品要求
- 醫(yī)院艾灸知識(shí)培訓(xùn)課件
- 2025年高考語(yǔ)文備考之文學(xué)類文本閱讀:敘述視角專項(xiàng)限時(shí)練
- 起重機(jī)司機(jī)Q2(限橋式起重機(jī))題庫(kù)題庫(kù)(1727道)
- 第九章 西半球的國(guó)家 單元教學(xué)設(shè)計(jì)-2023-2024學(xué)年七年級(jí)地理下學(xué)期人教版
- 云南錫業(yè)職業(yè)技術(shù)學(xué)院《影視劇配音》2023-2024學(xué)年第一學(xué)期期末試卷
- 2025年遼寧沈陽(yáng)地鐵集團(tuán)有限公司招聘筆試參考題庫(kù)含答案解析
- 體檢中心接待規(guī)范流程
- 夏季食堂食品安全注意事項(xiàng)
- 2025年全國(guó)水務(wù)集團(tuán)招聘筆試參考題庫(kù)含答案解析
- 知識(shí)產(chǎn)權(quán)合規(guī)管理體系解讀
評(píng)論
0/150
提交評(píng)論