欢迎来到天天文库
浏览记录
ID:16030263
大小:50.00 KB
页数:6页
时间:2018-08-07
《第5章 继承与接口》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、第5章继承与接口实验1继承2.模板代码Example.javaclassPeople{protecteddoubleweight,height;publicvoidspeakHello(){System.out.println("yayawawa");}publicvoidaverageHeight(){height=173;System.out.println("averageheight:"+height);}publicvoidaverageWeight(){weight=70;Syste
2、m.out.println("averageweight:"+weight);}}classChinaPeopleextendsPeople{publicvoidspeakHello(){System.out.println("你好,吃饭了吗?");}//重写publicvoidspeakHello()方法,要求输出类似“你好,吃饭了吗”//这样的汉语信息publicvoidaverageHeight(){height=168.78;System.out.println("中国人的平均身高:"+
3、height+"厘米");}//重写publicvoidaverageHeight()方法,要求输出类似//“中国人的平均身高:168.78厘米”这样的汉语信息publicvoidaverageWeight(){weight=65;System.out.println("中国人的平均体重:"+weight+"公斤");}//重写publicvoidaverageWeight()方法,要求输出类似//“中国人的平均体重:65公斤”这样的汉语信息publicvoidchinaGongfu(){Sys
4、tem.out.println("坐如钟,站如松,睡如弓");//输出中国武术的信息,如“坐如钟,站如松,睡如弓”等}}classAmericanPeopleextendsPeople{publicvoidspeakHello(){System.out.println("Howdoyoudo?");}//重写publicvoidspeakHello()方法,要求输出类似“Howdoyoudo”这样的英语信息publicvoidaverageHeight(){}//重写publicvoidaver
5、ageHeight()方法publicvoidaverageWeight(){}//重写publicvoidaverageWeight()方法publicvoidamericanBoxing(){System.out.println("直拳、钩拳");//输出拳击的信息,如“直拳”、“钩拳”等}}classBeijingPeopleextendsChinaPeople{publicvoidspeakHello(){System.out.println("您好?");}//重写publicvoid
6、speakHello()方法,要求输出类似“您好”这样的汉语信息publicvoidaverageHeight(){}//重写publicvoidaverageHeight()方法publicvoidaverageWeight(){}//重写publicvoidaverageWeight()方法publicvoidbeijingOpera(){System.out.println("京剧");//输出京剧的信息}}publicclassExample{publicstaticvoidmain(S
7、tringargs[]){ChinaPeoplechinaPeople=newChinaPeople();AmericanPeopleamericanPeople=newAmericanPeople();BeijingPeoplebeijingPeople=newBeijingPeople();chinaPeople.speakHello();americanPeople.speakHello();beijingPeople.speakHello();chinaPeople.averageHei
8、ght();americanPeople.averageHeight();beijingPeople.averageHeight();chinaPeople.averageWeight();americanPeople.averageWeight();beijingPeople.averageWeight();chinaPeople.chinaGongfu();americanPeople.americanBoxing();beijingPeople.beijingOpera();beijing
此文档下载收益归作者所有