欢迎来到天天文库
浏览记录
ID:35342269
大小:71.29 KB
页数:7页
时间:2019-03-23
《实验二继承与接口实验》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、实验二继承与接口—、实验目的1.掌握类的继承机制。2.熟悉类中成员变量和方法的访问控制。3.掌握接口与包的使用,熟悉方法的多态性。二、实验内容1.定义父类及子类,在子类中重写父类的方法2.练习接口与包的使用三、•实验步骤与要求第1题继承编写一个Java应用程序,除了主类外,该程序中还有4个类:People,ChinaPeople,AmericanPeople和BeijingPeople类。此四个类的继承关系如下图所示:要求ChinaPeople,American类均重写其父类People类的speakHello,averageHeight,av
2、erageWeight方法,BeijingPeople类重写其父类ChinaPeople类的speakHello,averageHeight,averageWeight方法。People类变量protecteddoubleheightprotecteddoubleweightChinaPeople类方法:publicvoidchinaGongfu()BeijingPeople类方法:PublicvoidbcijingOpcra()方法publicvoidspeakHello()publicvoidaverageHeight()publicvoi
3、davcragcWcight()AmericanPeople类方法:PublicvoidamcricanBoxing()源代码:packagepeople;classpeople{protecteddoubleheight;protecteddoubleweight;publicvoidspeakHello()//问候语的函数System.out.println("hello");}publicvoidaverageHeight()//人们的平均身高{height=170;System・out・printIn(+height);}publicv
4、oidaverageweight()//人们的平均体IE{weight=120;System・out・printIn(+weight);}}classChinapeopleextendspeople{publicvoidspeakHello(){System.out•printin(”你好“);}publicvoidaverageHeight(){height=172;System・out・printIn(+height);}publicvoidaverageweight(){weight=115;System・out・printIn(+wei
5、ght);}publicvoidchinaGongfu()//'
6、1国功夫的方法{System.out.printin("中国功夫,,);}}classAmericanpeopleextendspeople{publicvoidspeakHello(){System,out.printin(,,hello");}publicvoidaverageHeight()height=180;System.out.println(+height);}publicvoidaverageWeight()weight=150;System•out•printI
7、n(+weight);}publicvoidamericanBoxing()//^国拳击的方法System.out•println("americanBoxing");classBeijingpeopleextendsChinapeoplepublicvoidspeakHello()System.out.printIn(n北京欢迎你”);}publicvoidaverageHeight()height=168;System.out.println(+height);}publicvoidaverageWeight(){weight=125;Sy
8、stem•out.printIn(+weight);classExample{publicstaticvoidmain(String[]args)peoplep=newpeople();Chinapeoplec=newChinapeople();Americanpeoplea=newAmericanpeople();Beijingpeopleb=newBeijingpeople();p•averageHeight();p•averageWeight();р.speakHello();c•averageHeight();c•averageWeig
9、ht();с.chinaGongfu();c.speakHello();a•averageHeight();a•averageWeight();a•a
此文档下载收益归作者所有