欢迎来到天天文库
浏览记录
ID:41469250
大小:55.82 KB
页数:3页
时间:2019-08-25
《上机实践3继承与接口》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、实验三继承与接口一.实验目的巩固如下概念:子类的继承性、子类对象的创建过程、成员变量的继承与隐藏、方法的继承与重写;学握上转型对象的使用;学握接口冋调技术。二.实验要求实验前,应事先熟悉相关知识点,拟出相应的实验操作步骤,明确实验冃的和要求;实验过程中,服从实验指导教师安排,遵守实验室的各项规章制度,爱护实验仪器设备;实验操作完成后,认真书写实验报告,总结实验经验,分析实验过程中出现的问题。三.实验内容1、继承编写一个Java应用程序,除主类外,该程序屮还有4个类:People、ChinaPeople>Amer
2、icanPeople和BeijingPeople类。要求如下:People类有访问权限是protected的double型成员变量:height和weight,以及publicvoidspeakHelloO>publicvoidaverageHeight()>publicvoidaverageWeight()方法;ChinaPeople类是People的子类,新增了publicvoidchinaGongfuO方法。要求ChinaPeople重写父类的publicvoidspeakHelloO、publicvoi
3、daverageHeight()、publicvoidaverageWeight()方法;AmericanPeople类是People的子类,新增publicvoidamericanBoxing()方法。要求AmericanPeople重写父类的publicvoidspeakHelloO、publicvoidaverageHeight()、publicvoidavcragcW'cight()方法;BeijingPeople类是ChinaPeople的子类,新增publicvoidbeijingOpera()方法
4、。要求BeijingPeople重写父类的publicvoidspeakHelloOpublicvoidaveragelleight()、publicvoidaverageWeight()方法;请按模板要求,将【代码】替换为Java程序代码。Example.javaclassPeople{protecteddoubleweight,height;publicvoidspeakHello()System,out.prin11n(/zyayavawaz/);publicvoidaverageHeight()hei
5、ght=173;System,out.printin(''averageheight:z/+height);}publicvoidaverageWeight(){weight=70;System,out.println("averageweight:,?+weight);}}classChinaPeopleextendsPeople{【代码1】//重写publicvoidspeakllello()方法【代码2】//重写publicvoidaverageHeight()方法【代码3】//重写publicvoida
6、verageWeight()方法publicvoidchinaGongfu(){【代码4】//输出中国武术的信息}}classAmericanPeopleextendsPeople{【代码5】//重写publicvoidspeakHelloO方法【代码6】//重写publicvoidaverageHeight()方法【代码7】//重写publicvoidaverageWeight()方法publicvoidamericanBoxingO{【代码8】//输出拳击的信息}}classBeijingPeopleext
7、endsChinaPeople{【代码9】//重写publicvoidspeakllello()方法【代码10】〃重写publicvoidaverageHeight()方法【代码11】〃重写publicvoidaverageWeight()方法publicvoidbeijingOpera(){【代码12】//输出京剧的信息}publicclassExamplepublicstaticvoidmain(Stringargs[])ChinaPeoplechinaPeople二newChinaPeople();Ame
8、ricanPeopleamericanPeople二newAmericanPeople();BeijingPeoplebeijingPeople=newBeijingPeople();chinaPeople.speakHelloO;americanPcoplc.speakHello();beijingPeople・speakHello();chinaPeople.averageHeig
此文档下载收益归作者所有