第八次實驗報告_第1頁
第八次實驗報告_第2頁
第八次實驗報告_第3頁
第八次實驗報告_第4頁
第八次實驗報告_第5頁
已閱讀5頁,還剩6頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、第八次實驗實驗1:中國人、北京人和美國人1.實驗要求:編寫程序模擬中國人、美國人是人,北京人是中國人。除主類外,程序中還有4個類:People、ChinaPeople、AmericanPeople和BeijingPeople 類。要求如下:(1) People類有權(quán)限是protected的double型成員變量height和weight,以及public void speakHello()、public void averageHeight()和public void averageWeight()方法。(2) ChinaPeople類是People的子類,新增了public void ave

2、rageHeight()和public voidaverageWeight()方法。(3) AmericanPeople類是People的子類,新增方法public void AmericanBoxing() 。要求AmericanPeople重寫父類的public void speakHello()、public void averageHeight()和public void averageWeight()方法。(4) BeijingPeople類是ChinaPeople的子類,新增public void beijingOpera()方法。2.實驗代碼:/People.javapublic

3、 class People protected double weight,height;public void speakHello() System.out.println(yayayaya);public void averageHeight() height=173;System.out.println(average height:+height);public void averageWeight() weight=70;System.out.println(average weight:+weight);/ChinaPeople.javapublic class ChinaPeo

4、ple extends People public void speakHello() System.out.println(您好);public void averageHeight() height=168.78;System.out.println(中國人的平均身高:+height+厘米);public void averageWeight() weight=65;System.out.println(中國人的平均體重:+weight+千克);public void chinaGongfu() System.out.println(坐如鐘,站如松,睡如弓);/AmericanPeople

5、.javapublic class AmericanPeople extends People public void speakHello () System.out.println(How do you do);public void averageHeight() height=176;System.out.println(Americans average height:+height+厘米);public void averageWeight() weight=75;System.out.println(Americans average weight:+weight+ kg);pu

6、blic void americanBoxing() System.out.println(直拳,勾拳,組合拳);/BeijingPeople.javapublic class BeijingPeople extends ChinaPeople public void averageHeight() height=172.5;System.out.println(北京人的平均身高:+height+厘米);public void averageWeight() weight=70;System.out.println(北京人得平均體重:+weight+千克);public void beijin

7、gOpera() System.out.println(花臉、青衣、花旦和老生);/Example.javapublic class Example public static void main(String arg) ChinaPeople chinaPeople=new ChinaPeople();AmericanPeople americanPeople=new AmericanPeople();BeijingPeople beijingPeople=new BeijingPeople();chinaPeople.speakHello();americanPeople.speakHel

8、lo();beijingPeople.speakHello();chinaPeople.averageHeight();americanPeople.averageHeight();beijingPeople.averageHeight();chinaPeople.averageWeight();americanPeople.averageWeight();beijingPeople.averageWeight();chinaPeople.chinaGongfu();americanPeople.americanBoxing();beijingPeople.beijingOpera();bei

9、jingPeople.chinaGongfu();3.實驗結(jié)果:4.實驗分析:(1) 方法重寫時要保證方法的名字、類型、參數(shù)的個數(shù)和類型同父類的某個方法完全想同。這樣,子類繼承的方法才能被隱藏。(2) 子類在重寫方法時,如果重寫的方法是static方法,static關(guān)鍵字必須保留;如果重寫的方法是實例方法,重寫時不可以用static修飾。(3) 如果子類可以繼承父類的方法,子類就有權(quán)利重寫這個方法,子類通過重寫父類的方法可以改變父類的具遺體行為。5.實驗后的練習(xí):People類中的public void speakHello() public void averageHeight() publ

10、ic void averageWeight() 三個方法的方法體中的語句是否可以省略。答:可以省略,因為省略后結(jié)果沒有變化 實驗2:銀行計算利息1.實驗要求:假設(shè)銀行bank已經(jīng)有了按整年year計算利息的一般方法,其中year只能取正整數(shù)。比如,按整年計算的方法:Double computerInternet()Interest=year*0.35*saveMoney;Return interest;建設(shè)銀行constructionBank是bankde 子類,準備隱藏繼承的成員變量year,并重寫計算利息的方法,即自己聲明一個double型的year變量。要求constructionban

11、k和bankofDalian類是bank類的子類,constructionbank和bankofdalian都使用super調(diào)用隱藏的按整年計算利息的方法。2.實驗代碼:/Bank.javapublic class Bankint savedMoney;int year;double interest;double interestRate=0.29;public double computerInterest()interest=year*interestRate*savedMoney;return interest;public void setInterestRate( double r

12、ate)interestRate=rate;/ ConstructionBank.javapublic class ConstructionBank extends Bankdouble year;public double computerInterest()super.year=(int)year;double r=year-(int)year;int day=(int)(r*1000);double yearInterest=puterInterest();double dayInterest=day*0.0001*savedMoney;interest=yearInterest+day

13、Interest;System.out.printf(%d元存在建設(shè)銀行%d年零%d天的利息:%f元n,savedMoney,super.year,day,interest);return interest;/ BankOfDalian.javapublic class BankOfDalian extends Bank double year; public double computerInterest() super.year=(int)year; double r=year-(int)year; int day=(int)(r*1000); double yearInterest=pu

14、terInterest(); double dayInterest=day*0.00012*savedMoney; interest=yearInterest+dayInterest; System.out.printf(%d元存在大連銀行%d年零%d天的利息:%f元n,savedMoney,super.year,day,interest); return interest; / SaveMoney.javapublic class SaveMoneypublic static void main(String args)int amount=8000;ConstructionBank ban

15、k1=new ConstructionBank();bank1.savedMoney=amount;bank1.year=8.236;bank1.setInterestRate(0.035);double interest1=puterInterest();BankOfDalian bank2=new BankOfDalian();bank2.savedMoney=amount;bank2.year=8.236;bank2.setInterestRate(0.035);double interest2=puterInterest();System.out.printf(兩個銀行利息相差%f元n

16、,interest2-interest1);3.實驗結(jié)果:4.實驗分析:(1) 子類不繼承父類的構(gòu)造方法,因此子類在其構(gòu)造方法中需使用super來調(diào)用父類的構(gòu)造方法,并且super必須是子類構(gòu)造方法中的頭一條語句。(2) 當(dāng)super調(diào)用被隱藏的方法時,該方法中出現(xiàn)的成員變量是被子類隱藏的成員變量或繼承的成員變量。5.實驗后的練習(xí):參照建設(shè)銀行或大連銀行,在編寫一個商業(yè)銀行,讓程序輸出8000元存在商業(yè)銀行8年零236天的利息。/Bank.javapublic class Bankint savedMoney;int year;double interest;double interestRa

17、te=0.29;public double computerInterest()interest=year*interestRate*savedMoney;return interest;public void setInterestRate( double rate)interestRate=rate;/ CommercialBankpublic class CommercialBank extends Bank double year; public double computerInterest() super.year=(int)year; double r=year-(int)yea

18、r; int day=(int)(r*1000); double yearInterest=puterInterest(); double dayInterest=day*0.00012*savedMoney; interest=yearInterest+dayInterest; System.out.printf(%d元存在商業(yè)銀行%d年零%d天的利息:%f元n,savedMoney,super.year,day,interest); return interest; / SaveMoney.java public class SaveMoneypublic static void main

19、(String args)int amount=8000;CommercialBank bank=new CommercialBank();bank.savedMoney=amount;bank.year=8.236;bank.setInterestRate(0.035);double interest=puterInterest();實驗3:公司支出的總薪水1.實驗要求:要求有一個abstract類,類名為Employee。Employee的子類有YearWorker、MonthWorker、WeekWorker。YearWorker對象按年領(lǐng)取薪水,MonthWorker按月領(lǐng)取薪水、We

20、ekWorker按周領(lǐng)取的薪水。Employee類有一個abstract方法:public abstract earnings();子類必須重寫父類的earings()方法,給出各自領(lǐng)取報酬的具體方式。有一個Company類,該類用Employee對象數(shù)組作為成員,Employee對象數(shù)組的單元可以是YearWorker對象的上轉(zhuǎn)型對象、MonthWorker獨享的上轉(zhuǎn)型獨享或weekworker對象的上轉(zhuǎn)型獨享。程序能輸出Company對象一年需要支付的薪水總額。2.實驗代碼:abstract class Employeepublic abstract double earnings();c

21、lass YearWorker extends Employeepublic double earnings()return 12000;class MonthWorker extends Employeepublic double earnings()return 12*2300;class WeekWorker extends Employeepublic double earnings()return 52*780;class CompanyEmployee employee;double salaries=0;Company(Employee employee)this.employee=employee;public double salariesPay()salaries=0;for(int i=0;iemployee.length;i+)salaries=salaries+employeei.earnings();return salaries;public class CompanySalarypublic static void main(String args)Employee employee=new Employee29;for(int i=0;iemployee

溫馨提示

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

評論

0/150

提交評論