欢迎来到天天文库
浏览记录
ID:37852617
大小:155.50 KB
页数:8页
时间:2019-06-01
《java上机实验-类》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Java上机31掌握类的继承并应用,要求:(1)定义一个People类,该类具有类型为double,访问权限是protected的成员变量:height和weight;以及方法:publicvoidspeakHello(),publicvoidaverageHeight()和publicvoidaverageWeight()。(2)定义一个ChinaPeople类,,它是People的子类,新增了publicvoidchinaGongfu()方法,并要求ChinaPeople类重写父类的publicvoidspeakHello(
2、),publicvoidaverageHeight()和publicvoidaverageWeight()方法。(3)定义一个BeijingPeople类,,它是ChinaPeople的子类,新增了publicvoidbeijingOpera()方法,并要求BeijingPeople类重写父类的publicvoidspeakHello(),publicvoidaverageHeight()和publicvoidaverageWeight()方法。(4)定义一个主类,在主类中用上述类生成对象以及使用对象。classPeople{p
3、rotecteddoubleheight;protecteddoubleweight;publicvoidspeakHello(){System.out.println("Hello,我是people类!");}publicvoidaverageHeight(){height=165;System.out.println("people平均身高为"+height+"厘米");}publicvoidaverageWeight(){weight=150;System.out.println("people平均体重为"+weight+
4、"斤");}}classChinaPeopleextendsPeople{publicvoidchinaGongfu(){System.out.println("Ilikechinesegongfu!");}publicvoidspeakHello(){System.out.println("Hello,我是ChinaPeople类!");}publicvoidaverageHeight(){height=175;System.out.println("ChinaPeople平均身高为"+height+"厘米");}public
5、voidaverageWeight(){weight=145;System.out.println("ChinaPeople平均体重为"+weight+"斤");}}classBeijingPeopleextendsChinaPeople{publicvoidbeijingOpera(){System.out.println("我会唱京剧!");}publicvoidspeakHello(){System.out.println("Hello,我是BeijingPeople类!");}publicvoidaverageHeigh
6、t(){height=168;System.out.println("BeijingPeople平均身高为"+height+"厘米");}publicvoidaverageWeight(){weight=148;System.out.println("BeijingPeople平均体重为"+weight+"斤");}}publicclassE31{publicstaticvoidmain(Stringargs[]){Peoplepeople=newPeople();ChinaPeoplechinapeople=newChinaP
7、eople();BeijingPeoplebeijingpeople=newBeijingPeople();people.speakHello();people.averageHeight();people.averageWeight();chinapeople.chinaGongfu();chinapeople.speakHello();chinapeople.averageHeight();chinapeople.averageWeight();beijingpeople.chinaGongfu();beijingpeopl
8、e.beijingOpera();beijingpeople.speakHello();beijingpeople.averageHeight();beijingpeople.averageWeight();}}2掌握抽象类的应用,要求:定义抽象类Shape,有
此文档下载收益归作者所有