




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(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è)方法,子類通過重寫父類的方法可以改變父類的具遺體行為。5.實(shí)驗(yàn)后的練習(xí):People類中的publicvoidspeakHello()publicvoidaverageHeight()publicvoidaverageWeight()三個(gè)方法的方法體中的語句是否可以省略答:可以省略,因?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無P個(gè)4艮廳剎息相差3?.760086元Pressanj;kei?tocontinue■?■.實(shí)驗(yàn)分析:(1) 子類不繼承父類的構(gòu)造方法,因此子類在其構(gòu)造方法中需使用super來調(diào)用父類的構(gòu)造方法,并且super必須是子類構(gòu)造方法中的頭一條語句。(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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025屆河北省唐山市高三下學(xué)期第一次模擬考試政治試題(原卷版+解析版)
- 《商業(yè)插畫創(chuàng)意與表現(xiàn)》課件-【5】商業(yè)插畫的材料與表現(xiàn)技法
- 一體化污水處理設(shè)備采購安裝及運(yùn)維 投標(biāo)方案(技術(shù)方案)
- 三農(nóng)村基層教育資源配置與優(yōu)化方案
- 教育行業(yè)教師培訓(xùn)與成長(zhǎng)計(jì)劃
- 水利工程安全措施實(shí)施方案
- 工作計(jì)劃執(zhí)行跟蹤表格:工作計(jì)劃執(zhí)行情況統(tǒng)計(jì)表
- 三農(nóng)教育培訓(xùn)資源建設(shè)指南
- 軍用危險(xiǎn)品運(yùn)輸保密合同
- 農(nóng)村基層法制建設(shè)作業(yè)指導(dǎo)書
- 踝關(guān)節(jié)扭傷康復(fù)治療
- FZ∕T 01085-2018 粘合襯剝離強(qiáng)力試驗(yàn)方法
- 白龍江引水工程環(huán)境影響報(bào)告書(公示版)
- 《短視頻拍攝與制作》課件-3短視頻中期拍攝
- 瀏陽煙花術(shù)語大全
- 五星級(jí)酒店前廳管理常用表格
- 居民心理健康知識(shí)講座課件
- 《養(yǎng)老護(hù)理員》-課件:老年人安全防范及相關(guān)知識(shí)
- 2024年英語專業(yè)四級(jí)考試真題及詳細(xì)答案
- 成語故事葉公好龍
- MHT:中小學(xué)生心理健康檢測(cè)(含量表與評(píng)分說明)
評(píng)論
0/150
提交評(píng)論