第八次实验报告

上传人:枫** 文档编号:488679084 上传时间:2023-08-21 格式:DOC 页数:10 大小:107KB
返回 下载 相关 举报
第八次实验报告_第1页
第1页 / 共10页
第八次实验报告_第2页
第2页 / 共10页
第八次实验报告_第3页
第3页 / 共10页
第八次实验报告_第4页
第4页 / 共10页
第八次实验报告_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《第八次实验报告》由会员分享,可在线阅读,更多相关《第八次实验报告(10页珍藏版)》请在金锄头文库上搜索。

1、第八次实验实验1:中国人、北京人和美国人1.实验要求:编写程序模拟中国人、美国人是人,北京人是中国人。除主类外,程序中还有4个类:People、ChinaPeople、AmericanPeople和BeijingPeople 类。要求如下:(1) People类有权限是protected的double型成员变量height和weight,以及public void speakHello、public void averageHeight和public void averageWeight方法。(2) ChinaPeople类是People的子类,新增了public void averageHe

2、ight和public voidaverageWeight方法。(3) AmericanPeople类是People的子类,新增方法public void AmericanBoxing 。要求AmericanPeople重写父类的public void speakHello、public void averageHeight和public void averageWeight方法。(4) BeijingPeople类是ChinaPeople的子类,新增public void beijingOpera方法。2.实验代码:/People.javapublic class People protec

3、ted double weight,height;public void speakHello() System.out.println(yayayaya);public void averageHeight() height=173;System.out.println(average height:+height);public void averageWeight() weight=70;System.out.println(average weight:+weight);/ChinaPeople.javapublic class ChinaPeople extends People p

4、ublic void speakHello() (您好);public void averageHeight() height=168.78;System.out.println(中国人的平均身高:+height+厘米);public void averageWeight() weight=65;System.out.println(中国人的平均体重:+weight+千克);public void chinaGongfu() System.out.println(坐如钟,站如松,睡如弓);/AmericanPeople.javapublic class AmericanPeople exten

5、ds People public void speakHello () System.out.println(How do you do);public void averageHeight() height=176;System.out.println(Americans average height:+height+厘米);public void averageWeight() weight=75;System.out.println(Americans average weight:+weight+ kg);public void americanBoxing() System.out.

6、println(直拳,勾拳,组合拳);/BeijingPeople.javapublic class BeijingPeople extends ChinaPeople public void averageHeight() height=172.5;System.out.println(北京人的平均身高:+height+厘米);public void averageWeight() weight=70;System.out.println(北京人得平均体重:+weight+千克);public void beijingOpera() System.out.println(花脸、青衣、花旦和老

7、生);/Example.javapublic class Example public static void main(String arg) ChinaPeople chinaPeople=new ChinaPeople();AmericanPeople americanPeople=new AmericanPeople();BeijingPeople beijingPeople=new BeijingPeople();chinaPeople.speakHello();americanPeople.speakHello();beijingPeople.speakHello();chinaP

8、eople.averageHeight();americanPeople.averageHeight();beijingPeople.averageHeight();chinaPeople.averageWeight();americanPeople.averageWeight();beijingPeople.averageWeight();chinaPeople.chinaGongfu();americanPeople.americanBoxing();beijingPeople.beijingOpera();beijingPeople.chinaGongfu();3.实验结果:4.实验分析

9、:(1) 方法重写时要保证方法的名字、类型、参数的个数和类型同父类的某个方法完全想同。这样,子类继承的方法才能被隐藏。(2) 子类在重写方法时,如果重写的方法是static方法,static关键字必须保存;如果重写的方法是实例方法,重写时不可以用static修饰。(3) 如果子类可以继承父类的方法,子类就有权利重写这个方法,子类通过重写父类的方法可以改变父类的具遗体行为。5.实验后的练习:People类中的public void speakHello() public void averageHeight() public void averageWeight() 三个方法的方法体中的语句是否

10、可以省略。答:可以省略,因为省略后结果没有变化 实验2:银行计算利息1.实验要求:假设银行bank已经有了按整年year计算利息的一般方法,其中year只能取正整数。比方,按整年计算的方法:Double computerInternet()Interest=year*0.35*saveMoney;Return interest;建设银行constructionBank是bankde 子类,准备隐藏继承的成员变量year,并重写计算利息的方法,即自己声明一个double型的year变量。要求constructionbank和bankofDalian类是bank类的子类,constructionb

11、ank和bankofdalian都使用super调用隐藏的按整年计算利息的方法。2.实验代码:/Bank.javapublic class Bankint savedMoney;int year;double interest;double interestRate=0.29;public double computerInterest()interest=year*interestRate*savedMoney;return interest;public void setInterestRate( double rate)interestRate=rate;/ ConstructionBa

12、nk.javapublic class ConstructionBank extends Bankdouble year;public double computerInterest()super.year=(int)year;double r=year-(int)year;int day=(int)(r*1000);double yearInterest=super puterInterest();double dayInterest=day*0.0001*savedMoney;interest=yearInterest+dayInterest;System.out.printf(%d元存在

13、建设银行%d年零%d天的利息:%f元n,savedMoney,super.year,day,interest);return interest;/ BankOfDalian.javapublic class BankOfDalian extends Bank double year; public double computerInterest() super.year=(int)year; double r=year-(int)year; int day=(int)(r*1000); double yearInterest=super puterInterest(); double dayI

14、nterest=day*0.00012*savedMoney; interest=yearInterest+dayInterest; System.out.printf(%d元存在大连银行%d年零%d天的利息:%f元n,savedMoney,super.year,day,interest); return interest; / SaveMoney.javapublic class SaveMoneypublic static void main(String args)int amount=8000;ConstructionBank bank1=new ConstructionBank();bank1.savedMoney=amount;bank1.year=8.236;bank1.setInterestRate(0.035);double interest1=bank1 puterInter

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 模板/表格 > 财务表格

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号