




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
實驗報告三課程JAVA語言程序設計實驗項目繼承與接口成績學號XXXXXX姓名XXXXX實驗日期2012/10/13專業班級計算機科學與技術09級指導教師XXXXX一【實驗目的】掌握類和對象的創建方法理解繼承的相關概念掌握子類對象的創建過程掌握方法的繼承與重寫理解多態的概念與上轉型對象理解抽象類和接口的概念及特點二【實驗內容】【項目一】方法重寫定義父類People,分別定義People類的子類ChinaPeopleAmericanPeople和BeijingPeople并分別重寫父類中的各個方法。最后在主方法中分別創建各子類的對象并調用各自的方法打印輸出信息。該程序的模板代碼見附件/Example.java請將其補充完整并調試運行。(1)程序代碼classPeople{protecteddoubleweight,height;publicvoidspeakHello(){System.out.println("yayawawa");}publicvoidaverageHeight(){height=173;System.out.println("averageheight:"+height);}publicvoidaverageWeight(){weight=70;System.out.println("averageweight:"+weight);}}classChinaPeopleextendsPeople{//doubleweight,height;//重寫publicvoidspeakHello(方法,要求輸出類似“你好,吃了嗎”這樣的//漢語信息publicvoidspeakHello(){System.out.println(你好,你吃了嗎?");}//重寫publicvoidaverageHeight(方法,要求輸出類似〃“中國人的平均身高:168.78厘米”這樣的漢語信息publicvoidaverageHeight(){height=168.78;System.out.println(中國人的平均身高”+height);}//重寫publicvoidaverageWeight(方法,//要求輸出類似“中國人的平均體重:65公斤”這樣的漢語信息publicvoidaverageWeight(){weight=65;System.out.println(中國人的平均體重"+weight);}publicvoidchinaGongfu(){//輸出中國武術的信息,例如:〃坐如鐘,站如松睡如弓〃等System.out.println(坐如鐘,站如松,睡如弓);}}classAmericanPeopleextendsPeople{//重寫publicvoidspeakHello(方法,要求輸出類似//“Howdoyoud6'這樣的英語信息。publicvoidspeakHello(){System.out.println("Howdoyoudo");}//重寫publicvoidaverageHeight(方法publicvoidaverageHeight(){height=173;System.out.println("Americanaverageheight:"+height);}//重寫publicvoidaverageWeight方法publicvoidaverageWeight(){weight=70;System.out.println("Americanaverageweight:"+weight);}publicvoidamericanBoxing(){//輸出拳擊的信息,例如,“直拳”、“鉤拳”等System.out.println(直拳,上勾拳');}}classBeijingPeopleextendsChinaPeople{//重寫publicvoidspeakHello(方法,要求輸出類似“您好”這樣的漢語信息publicvoidspeakHello(){System.out.println(您好啊!!!!!!!!!!!!");}//重寫publicvoidaverageHeight(方法publicvoidaverageHeight(){height=175;System.out.println(北京人的平均身高"+height);}//重寫publicvoidaverageWeight(方法publicvoidaverageWeight(){weight=20;System.out.println(北京人的平均體重"+weight);}publicvoidbeijingOpera(){//輸出京劇的信息System.out.println(京劇?verygood!!");}}publicclassExample{publicstaticvoidmain(Stringargs[]){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();}}(2)運行結果D:\>jauacExample.JauaID:\>jauaExample你好,你吃了嗎?Howdoyoudo您好啊!!!![!!!!中國入的平均身高=168.78Americanaverageheight:173.0北京人南豐舟身高=175.0中國入的平均’Americanaverageweight:70.0北京人的平均體重=20-0坐知中,拒口松,睡如弓直拳,上勾拳廳、劇?verygood!!蟲易鐘,站如松,睡如弓【項目二】上轉型對象類Employee為抽象類,具有子類YearWorker,MonthWorke和MonthWorke。YearWorker對象按年領取薪水,MonthWorke^月領取,MonthWorker按周領取。Employee類有一個抽象方法:publicabstractearnings();子類必須重寫父類l^earningsC方法,給出各自領取報酬的具體方式。在Company類中,使用employee數組作為成員,使用不同的子類對象作為數組元素的上轉型對象。該程序的模板代碼見附件/HardWorkjava請將其補充完整并調試運行。(1)程序代碼abstractclassEmployee{publicabstractdoubleearnings();}classYearWorkerextendsEmployee{publicdoubleearnings(){return12000;}}classMonthWorkerextendsEmployee{publicdoubleearnings(){return850;}}classWeekWorkerextendsEmployee{publicdoubleearnings(){return150;}}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;}}publicclassHardWork{publicstaticvoidmain(Stringargs[]){Employee]]employee=newEmployee[20];for(inti=0;i<employee.length;i++){if(i%3==0)employee[i]=newWeekWorker();elseif(i%3==1)employee[i]=newMonthWorker();elseif(i%3==2)employee[i]=newYearWorker();}Companycompany=newCompany(employee);System.out.println(公司年工資總額〃+company.salariesPay());}}(2)運行結果D:\>jauacHardUork.jauaD:\>jauaHardUork公司年工資總額:79000-0【項目三】接口讀懂/附件/Road.java源程序,按要求補充程序并調試運行。掌握接口的定義及其實現方法,學習接口回調的運用方法。(1)程序代碼interfaceComputerWeight{publicdoublecomputeWeight();}classTelevisionimplementsComputerWeight{ //實現computeWeight()方法。publicdoublecomputeWeight(){return75.5;}}classComputerimplementsComputerWeight{ //實現computeWeight()方法。publicdoublecomputeWeight(){return90.5;}}classWashMachineimplementsComputerWeight{ //實現computeWeight(方法。publicdoublecomputeWeight(){return120.5;}}classCar{ComputerWeight]]goods;doubletotalWeights=0;Car(ComputerWeight[]goods){this.goods=goods;}publicdoublegetTotalWeights(){totalWeights=0;//計算totalWeightsfor(inti=0;i<goods.length;i++)totalWeights=totalWeights+goods[i].computeWeight();returntotalWeights;publicclassRoad{publicstaticvoidmain(Stringargs[]){ComputerWeight[]goodsOne=newComputerWeight[50],goodsTwo=newComputerWeight[22];for(inti=0;i<goodsOne.length;i++){ if(i%3==0)goodsOne[i]=newTelevision();elseif(i%3==1)goodsOne[i]=newComputer();elseif(i%3==2)goodsOne[i]=newWashMachine();}for(inti=0;i<goodsTwo.length;i++){ if(i%3==0)goodsTwo[i]=newTelevision();elseif(i%3==1)goodsTwo[i]=newComputer();elseif(i%3==2)goodsTwo[i]=newWashMachine();}Car大貨^=newCar(goodsOne);System.out.printl
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 冀教版數學八下20.1《常量和變量》模板
- 中信百信銀行java面試題及答案
- 融資證考試題及答案
- 農村居民受益于統一大市場
- 國有土地使用權出讓合同模板
- 房地產結構及立面優化設計合同模板
- 電力事故調查規程
- Brand KPIs for car insurance:VHV in Germany-英文培訓課件2025.5
- 心理師資建設
- 政治中亞峰會題目及答案
- 2025-2030中國膜電極組件(MEA)行業市場發展趨勢與前景展望戰略研究報告
- 2025年骨干教師考試題庫全
- 超市管理制度獎懲制度
- DB64-680-2025 建筑工程安全管理規程
- DB11 419-2007 電梯安裝維修作業安全規范
- 國有企業技能人才的職業發展路徑與激勵機制研究
- 金氏五行升降中醫方集
- 反應釜(容器)生產企業安全風險分級管控資料
- 2025年生物北京中考試題及答案
- 聚乳酸增韌改性技術進展研究
- 老年人心理健康課件
評論
0/150
提交評論