資源描述:
《實驗二 繼承與接口實驗.doc》由會員上傳分享,免費在線閱讀,更多相關內(nèi)容在教育資源-天天文庫。
1、實驗二繼承與接口一、實驗目的1.掌握類的繼承機制。2.熟悉類中成員變量和方法的訪問控制。3.掌握接口與包的使用,熟悉方法的多態(tài)性。二、實驗內(nèi)容1.定義父類及子類,在子類中重寫父類的方法2.練習接口與包的使用三、.實驗步驟與要求第1題繼承編寫一個Java應用程序,除了主類外,該程序中還有4個類:People,ChinaPeople,AmericanPeople和BeijingPeople類。此四個類的繼承關系如下圖所示:要求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