資源描述:
《實(shí)驗(yàn)二 繼承與接口實(shí)驗(yàn).doc》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、實(shí)驗(yàn)二繼承與接口一、實(shí)驗(yàn)?zāi)康?.掌握類的繼承機(jī)制。2.熟悉類中成員變量和方法的訪問控制。3.掌握接口與包的使用,熟悉方法的多態(tài)性。二、實(shí)驗(yàn)內(nèi)容1.定義父類及子類,在子類中重寫父類的方法2.練習(xí)接口與包的使用三、.實(shí)驗(yàn)步驟與要求第1題繼承編寫一個(gè)Java應(yīng)用程序,除了主類外,該程序中還有4個(gè)類:People,ChinaPeople,AmericanPeople和BeijingPeople類。此四個(gè)類的繼承關(guān)系如下圖所示:要求ChinaPeople,American類均重寫其父類People類的speakHello,averageHeight,averageWeig
2、ht方法,BeijingPeople類重寫其父類ChinaPeople類的speakHello,averageHeight,averageWeight方法。People類變量方法protecteddoubleheightpublicvoidspeakHello()protecteddoubleweightpublicvoidaverageHeight()publicvoidaverageWeight()ChinaPeople類方法:publicvoidchinaGongfu()AmericanPeople類方法:PublicvoidamericanBoxing(
3、)BeijingPeople類方法:PublicvoidbeijingOpera()源代碼:packagepeople;classpeople{protecteddoubleheight;protecteddoubleweight;publicvoidspeakHello()//問候語的函數(shù){System.out.println("hello");}publicvoidaverageHeight()//人們的平均身高{height=170;System.out.println(+height);}publicvoidaverageWeight()//人們的平均體
4、重{weight=120;System.out.println(+weight);}}classChinapeopleextendspeople{publicvoidspeakHello(){System.out.println("你好");}publicvoidaverageHeight(){height=172;System.out.println(+height);}publicvoidaverageWeight(){weight=115;System.out.println(+weight);}publicvoidchinaGongfu()//中國功夫的
5、方法{System.out.println("中國功夫");}}classAmericanpeopleextendspeople{publicvoidspeakHello(){System.out.println("hello");}publicvoidaverageHeight(){height=180;System.out.println(+height);}publicvoidaverageWeight(){weight=150;System.out.println(+weight);}publicvoidamericanBoxing()//美國拳擊的方法
6、{System.out.println("americanBoxing");}}classBeijingpeopleextendsChinapeople{publicvoidspeakHello(){System.out.println("北京歡迎你");}publicvoidaverageHeight(){height=168;System.out.println(+height);}publicvoidaverageWeight(){weight=125;System.out.println(+weight);}}classExample{publicsta
7、ticvoidmain(String[]args){peoplep=newpeople();Chinapeoplec=newChinapeople();Americanpeoplea=newAmericanpeople();Beijingpeopleb=newBeijingpeople();p.averageHeight();p.averageWeight();p.speakHello();c.averageHeight();c.averageWeight();c.chinaGongfu();c.speakHello();a.averageHeight();a.
8、averageWeigh