您好,欢迎访问三七文档
第八次实验实验1:中国人、北京人和美国人1.实验要求:编写程序模拟中国人、美国人是人,北京人是中国人。除主类外,程序中还有4个类:People、ChinaPeople、AmericanPeople和BeijingPeople类。要求如下:(1)People类有权限是protected的double型成员变量height和weight,以及publicvoidspeakHello()、publicvoidaverageHeight()和publicvoidaverageWeight()方法。(2)ChinaPeople类是People的子类,新增了publicvoidaverageHeight()和publicvoidaverageWeight()方法。(3)AmericanPeople类是People的子类,新增方法publicvoidAmericanBoxing()。要求AmericanPeople重写父类的publicvoidspeakHello()、publicvoidaverageHeight()和publicvoidaverageWeight()方法。(4)BeijingPeople类是ChinaPeople的子类,新增publicvoidbeijingOpera()方法。2.实验代码://People.javapublicclassPeople{protecteddoubleweight,height;publicvoidspeakHello(){System.out.println(yayayaya);}publicvoidaverageHeight(){height=173;System.out.println(averageheight:+height);}publicvoidaverageWeight(){weight=70;System.out.println(averageweight:+weight);}}//ChinaPeople.javapublicclassChinaPeopleextendsPeople{publicvoidspeakHello(){System.out.println(您好);}publicvoidaverageHeight(){height=168.78;System.out.println(中国人的平均身高:+height+厘米);}publicvoidaverageWeight(){weight=65;System.out.println(中国人的平均体重:+weight+千克);}publicvoidchinaGongfu(){System.out.println(坐如钟,站如松,睡如弓);}}//AmericanPeople.javapublicclassAmericanPeopleextendsPeople{publicvoidspeakHello(){System.out.println(Howdoyoudo);}publicvoidaverageHeight(){height=176;System.out.println(American'saverageheight:+height+厘米);}publicvoidaverageWeight(){weight=75;System.out.println(American'saverageweight:+weight+kg);}publicvoidamericanBoxing(){System.out.println(直拳,勾拳,组合拳);}}//BeijingPeople.javapublicclassBeijingPeopleextendsChinaPeople{publicvoidaverageHeight(){height=172.5;System.out.println(北京人的平均身高:+height+厘米);}publicvoidaverageWeight(){weight=70;System.out.println(北京人得平均体重:+weight+千克);}publicvoidbeijingOpera(){System.out.println(花脸、青衣、花旦和老生);}}//Example.javapublicclassExample{publicstaticvoidmain(Stringarg[]){ChinaPeoplechinaPeople=newChinaPeople();AmericanPeopleamericanPeople=newAmericanPeople();BeijingPeoplebeijingPeople=newBeijingPeople();chinaPeople.speakHello();americanPeople.speakHello();beijingPeople.speakHello();chinaPeople.averageHeight();americanPeople.averageHeight();beijingPeople.averageHeight();chinaPeople.averageWeight();americanPeople.averageWeight();beijingPeople.averageWeight();chinaPeople.chinaGongfu();americanPeople.americanBoxing();beijingPeople.beijingOpera();beijingPeople.chinaGongfu();}}3.实验结果:4.实验分析:(1)方法重写时要保证方法的名字、类型、参数的个数和类型同父类的某个方法完全想同。这样,子类继承的方法才能被隐藏。(2)子类在重写方法时,如果重写的方法是static方法,static关键字必须保留;如果重写的方法是实例方法,重写时不可以用static修饰。(3)如果子类可以继承父类的方法,子类就有权利重写这个方法,子类通过重写父类的方法可以改变父类的具遗体行为。5.实验后的练习:People类中的publicvoidspeakHello()publicvoidaverageHeight()publicvoidaverageWeight()三个方法的方法体中的语句是否可以省略。答:可以省略,因为省略后结果没有变化实验2:银行计算利息1.实验要求:假设银行bank已经有了按整年year计算利息的一般方法,其中year只能取正整数。比如,按整年计算的方法:DoublecomputerInternet(){Interest=year*0.35*saveMoney;Returninterest;}建设银行constructionBank是bankde子类,准备隐藏继承的成员变量year,并重写计算利息的方法,即自己声明一个double型的year变量。要求constructionbank和bankofDalian类是bank类的子类,constructionbank和bankofdalian都使用super调用隐藏的按整年计算利息的方法。2.实验代码://Bank.javapublicclassBank{intsavedMoney;intyear;doubleinterest;doubleinterestRate=0.29;publicdoublecomputerInterest(){interest=year*interestRate*savedMoney;returninterest;}publicvoidsetInterestRate(doublerate){interestRate=rate;}}//ConstructionBank.javapublicclassConstructionBankextendsBank{doubleyear;publicdoublecomputerInterest(){super.year=(int)year;doubler=year-(int)year;intday=(int)(r*1000);doubleyearInterest=super.computerInterest();doubledayInterest=day*0.0001*savedMoney;interest=yearInterest+dayInterest;System.out.printf(%d元存在建设银行%d年零%d天的利息:%f元\n,savedMoney,super.year,day,interest);returninterest;}}//BankOfDalian.javapublicclassBankOfDalianextendsBank{doubleyear;publicdoublecomputerInterest(){super.year=(int)year;doubler=year-(int)year;intday=(int)(r*1000);doubleyearInterest=super.computerInterest();doubledayInterest=day*0.00012*savedMoney;interest=yearInterest+dayInterest;System.out.printf(%d元存在大连银行%d年零%d天的利息:%f元\n,savedMoney,super.year,day,interest);returninterest;}}//SaveMoney.javapublicclassSaveMoney{publicstaticvoidmain(Stringargs[]){intamount=8000;ConstructionBankbank1=newConstructionBank();bank1.savedMoney=amount;bank1.year=8.236;bank1.setInterestRate(0.035);doubleinterest1=bank1.computerInterest();BankOfDalianbank2=newBankOfDalian();bank2.savedMoney=amount;bank2.year=8.236;bank2.setInterestRate(0.035);doubleinterest2=bank2.computerInterest();System.out.printf(两个银行利息相差%f元\n,interest2-interest1);}}3.实验结果:4.实验分析:(1)子类不继承父类的构造方法,因此子类在其构造方法中需使用super来调用父类的构造方法,并且super必须是子类构造方法中的头一条语句。(2)当super调用被隐藏的方法时,该方法中出现的成员变量是被子类隐藏的成员变量或继承的成员变量。5.实验后的练习:参照建设银行或大连银行,在编写一个商业银行,让程序输出8000元存在商业银行8年零236天的利息。//Bank.javapublicclassBank{intsavedMoney;intyear;doubleinterest;doubleinterestRate=0.29;publicdoublecomputerInterest(){interest=year*interestRate*savedMoney;returninterest;}publicvoidsetInterestRate(doublerate){interestRate=rate;}}//CommercialBankpublicclassCommercialBankextendsBank{doubleyear;publicdoublecomputerInterest(){super.year=(int)year;doubler=year-(int
本文标题:第八次实验报告
链接地址:https://www.777doc.com/doc-8104370 .html