欢迎来到天天文库
浏览记录
ID:56777160
大小:64.50 KB
页数:7页
时间:2020-07-09
《实验二 继承与接口实验.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、实验二继承与接口一、实验目的1.掌握类的继承机制。2.熟悉类中成员变量和方法的访问控制。3.掌握接口与包的使用,熟悉方法的多态性。二、实验内容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()//问候语的函数{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
此文档下载收益归作者所有