實(shí)驗(yàn)二繼承及接口實(shí)驗(yàn)_第1頁
實(shí)驗(yàn)二繼承及接口實(shí)驗(yàn)_第2頁
實(shí)驗(yàn)二繼承及接口實(shí)驗(yàn)_第3頁
實(shí)驗(yàn)二繼承及接口實(shí)驗(yàn)_第4頁
實(shí)驗(yàn)二繼承及接口實(shí)驗(yàn)_第5頁
已閱讀5頁,還剩1頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、-. z.實(shí)驗(yàn)二繼承與接口一、實(shí)驗(yàn)?zāi)康恼莆疹惖睦^承機(jī)制。熟悉類中成員變量和方法的訪問控制。掌握接口與包的使用,熟悉方法的多態(tài)性。二、實(shí)驗(yàn)容定義父類及子類,在子類中重寫父類的方法練習(xí)接口與包的使用三、.實(shí)驗(yàn)步驟與要求第1題繼承編寫一個Java應(yīng)用程序,除了主類外,該程序中還有4個類:People,ChinaPeople,AmericanPeople和BeijingPeople類。此四個類的繼承關(guān)系如下列圖所示:要求ChinaPeople,American類均重寫其父類People類的speakHello, averageHeight, averageWeight方法,BeijingPeople類

2、重寫其父類ChinaPeople類的speakHello, averageHeight, averageWeight方法。People類 變量 方法protected double height public void speak Hello() protected double weight public void averageHeight() public void averageWeight()ChinaPeople類方法: public void chinaGongfu()AmericanPeople類方法:Public void americanBo*ing()BeijingPeo

3、ple類方法:Public void beijingOpera()源代碼:package people;class peopleprotecteddoubleheight ; protecteddoubleweight ; publicvoid speakHello() /問候語的函數(shù)System.out.println(hello);publicvoid averageHeight()/人們的平均身高h(yuǎn)eight=170;System.out.println(+height);publicvoid averageWeight()/人們的平均體重 weight=120;System.out.p

4、rintln(+weight);class Chinapeople e*tends peoplepublicvoid speakHello() System.out.println(你好);publicvoid averageHeight()height=172;System.out.println(+height);publicvoid averageWeight() weight=115;System.out.println(+weight);publicvoid chinaGongfu()/中國功夫的方法System.out.println(中國功夫);class Americanpeo

5、ple e*tends peoplepublicvoid speakHello() System.out.println(hello);publicvoid averageHeight()height=180;System.out.println(+height);publicvoid averageWeight() weight=150;System.out.println(+weight);publicvoid americanBo*ing()/美國拳擊的方法System.out.println(americanBo*ing);class Beijingpeople e*tends Chi

6、napeoplepublicvoid speakHello() System.out.println(歡送你);publicvoid averageHeight()height=168;System.out.println(+height);publicvoid averageWeight() weight=125;System.out.println(+weight);class E*amplepublicstaticvoid main(String args)people p =new people();Chinapeople c=new Chinapeople();Americanpeo

7、ple a=new Americanpeople();Beijingpeople b=new Beijingpeople();p.averageHeight();p.averageWeight();p.speakHello();c.averageHeight();c.averageWeight();c.chinaGongfu();c.speakHello();a.averageHeight();a.averageWeight();a.americanBo*ing();a.speakHello();b.averageHeight();b.averageWeight();b.speakHello(

8、);結(jié)果截圖:第2題上轉(zhuǎn)型對象要求有一個abstract類,類名為Employee。Employee的子類有YearWorker,MonthWorker和WeekWorker。YearWorker按年領(lǐng)取薪水,MonthWorker按月領(lǐng)取薪水,WeekWorker按周領(lǐng)取薪水。Employee類有一個abstract方法:public abstract earnings();子類必須重寫父類的earnings()方法,給出各自領(lǐng)取報(bào)酬的具體方式。有一個pany類,該類用employee數(shù)組作為成員,employee數(shù)組的單元可以是YearWorker對象的上轉(zhuǎn)型對象、MonthWorker對

9、象的上轉(zhuǎn)型對象或WeekWorker對象的上轉(zhuǎn)型對象。程序能輸出pany對象一年需要支付的薪水總額。源代碼:package people;abstractclass Employee intsalary;publicabstractvoid earnings();class YearWorker e*tends Employee publicvoid earnings() salary=200;System.out.println(年薪: +salary*365);class MonthWorker e*tends Employee publicvoid earnings() salary=1

10、00;System.out.println(月薪:+salary*30+ 一年薪水: +salary*30*12);class WeekWorker e*tends Employee publicvoid earnings() salary=80;System.out.println(周薪:+salary*7+ 一年薪水: +salary*365);publicclass pany publicstaticvoid main(String args) Employee e=new Employee3; e0=new YearWorker();e0.earnings();e1=new Month

11、Worker();e1.earnings();e2=new WeekWorker();e2.earnings();結(jié)果截圖:第3題接口回調(diào)要求有一個puteTotalSales接口,該接口中有一個方法:public double totalSalesByYear(),有三個實(shí)現(xiàn)該接口的類:Television,puter和Mobile。這三個類通過實(shí)現(xiàn)接口puteTotalSales,給出自己的年銷售額。有一個Shop類,該類用puteTotalSales數(shù)組作為成員,puteTotalSales數(shù)組的單元可以存放Television對象的引用、puter對象的引用或Mobile對象的引用。程

12、序能輸出Shop對象的年銷售額。源代碼:package people;interface puteTotalSalespublicdouble totalSalesByYear();class Television implements puteTotalSalespublicdouble totalSalesByYear()doublesale=100;returnsale*365;class puter implements puteTotalSalespublicdouble totalSalesByYear()doublesale=200;returnsale*365;class Mo

13、bile implements puteTotalSalespublicdouble totalSalesByYear()doublesale=220;returnsale*365;publicclass shop publicstaticvoid main(String args) puteTotalSales c;c=new puteTotalSales3;c0=new Television(); System.out.println(電視機(jī)的年銷售量:+c0.totalSalesByYear();c1=new puter(); System.out.println(電腦的年銷售量:+c1.totalSalesByYear();

溫馨提示

  • 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論