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 类是 Peop
2、le 的子类,新增了 public void averageHeight ()和 public voidaverageWeight ()方法。(3)AmericanPeople 类是 People 的子类,新增方法 public void AmericanBoxing ()。要求 AmericanPeople 重写父类的 public void speakHello ()、public voidaverageHeight ()禾口 public void averageWeight () 方法。(4)BeijingPeople 类是 ChinaPeople 的子类,新增 public void
3、 beijingOpera ()方法。2实验代码:/People.javapublic class People protected double weight,height;public void speakHello。System.out.pri ntl n(yayayaya);public void averageHeight() height=173; System.out.println(average height:+height);public void averageWeight() weight=70; System.out.pri ntl n(average weight:
4、+weight);/Chi naPeople.javapublic class Chin aPeople exte nds People public void speakHello() System.out.println(” 您好);public void averageHeight() height=168.78; System.out.println(” 中国人的平均身高: +height+厘米);public void averageWeight() weight=65;System.out.println(” 中国人的平均体重: +weight+千克”);public void c
5、hinaGon gfu() System.out.println(”坐如钟,站如松,睡如弓 ”);/America nPeople.javapublic class America nPeople exte nds People public void speakHello () System.out.pri ntln (How do you do);public void averageHeight() height=176;System.out.println(Americans average height:+height+ 厘米); public void averageWeight(
6、) weight=75;System.out.println(Americans average weight:+weight+ kg);public void america nBox in g() System.out.println(” 直拳,勾拳,组合拳 ”);/Beiji ngPeople.javapublic class Beiji ngPeople exte nds Chin aPeople public void averageHeight() height=172.5;System.out.println(” 北京人的平均身高:+height+厘米);public void
7、averageWeight() weight=70;System.out.println(北京人得平均体重: +weight+千克);public void beiji ngOpera() System.out.println(”花脸、青衣、花旦和老生 ”);/Example.javapublic class Example public static void main(String arg) Chin aPeople chi naPeople=new Chin aPeople();America nPeople america nPeople=new America nPeople();B
8、eijingPeople beijingPeople=new BeijingPeople();chin aPeople.speakHello();america nPeople.speakHello();beiji ngPeople.speakHello();chin aPeople.averageHeight();america nPeople.averageHeight();beiji ngPeople.averageHeight();chin aPeople.averageWeight();america nPeople.averageWeight();beiji ngPeople.av
9、erageWeight();chin aPeople.ch in aGo ngfu();america nPeople.america nBox in g();beiji ngPeople.beiji ngOpera();beiji ngPeople.ch inaGon gfu();3.实验结果:4.实验分析:(1)方法重写时要保证方法的名字、类型、参数的个数和类型同父类的某个方法完全想同。 这样,子类继承的方法才能被隐藏。 子类在重写方法时,如果重写的方法是 static方法,static关键字必须保留;如果重写的(3)如果子类可以继承父类的方法,子类就有权利重写这个方法,子类通过重写父类的
10、方法 可以改变父类的具遗体行为。5实验后的练习:People类中的public void speakHello()public void averageHeight()public void averageWeight()三个方法的方法体中的语句是否可以省略。 答:可以省略,因为省略后结果没有变化实验2:银行计算利息1实验要求:假设银行bank已经有了按整年year计算利息的一般方法,其中 year只能取正整数。比如,按整年计算的方法:Double computeri ntern et()In terest=year*0.35*save Mon ey;Return interest;建设银行c
11、onstructionBank是bankde子类,准备隐藏继承的成员变量 year,并重写计算利息的方法,即自己声明一个 double型的year变量。要求constructionbank和bankofDalian类 是bank类的子类,constructionbank和bankofdalian都使用super调用隐藏的按整年计算利息 的方法。2.实验代码:/Ban k.javapublic class Bankint savedM on ey;int year;double in terest;double in terestRate=0.29;public double computerI
12、 nterest()in terest=year*i nterestRate*savedM on ey;return in terest;public void setI nterestRate( double rate)in terestRate=rate;/ Con structio nBan k.javapublic class Con struct ionBank exte nds Bankdouble year;public double computerI nterest()super.year=(i nt)year;double r=year-(i nt)year;int day
13、=(i nt)(r*1000);double yearI nterest=puterl nterest();double dayI nterest=day*0.0001*savedMo ney;in terest=yearI nterest+day In terest;System.out.printf(%d 元存在建设银行%d 年零 %d 天的禾U息:f 元n ,savedM on ey,super.year,day,i nterest);return in terest;/ Ban kOfDalia n.javapublic class Ban kOfDalia n exte nds Ba
14、nk double year;public double computerI nterest()super.year=(i nt)year;double r=year-(i nt)year;int day=(i nt)(r*1000);double yearI nterest=puterl nterest();double day In terest=day*0.00012*savedMo ney;in terest=yearI nterest+day In terest;System.out.printf(%d 元存在大连银行 %d 年零 %d 天的禾U息:f 元 n ,savedM on
15、ey,super.year,day,i nterest);return in terest;/ SaveMon ey.javapublic class SaveMon eypublic static void main(String args)int amou nt=8000;Con struct ionBank ban k1= new Con struct ionBan k();ban k1.savedM on ey=am ount;ban k1.year=8.236;ba nk1.setI nterestRate(0.035);double in terest1=ba puterl nte
16、rest();Ban kOfDalian ban k2=new Ban kOfDalia n();ban k2.savedM on ey=am ount;ban k2.year=8.236;ba nk2.setI nterestRate(0.035);double in terest2=ba puterl nterest();System.out.printf(两个银行利息相差 f 元 n”,interest2-interest1); 3.实验结果:4.实验分析:(1)子类不继承父类的构造方法,因此子类在其构造方法中需使用 super来调用父类的构造方法,并且super必须是子类构造方法中的头
17、一条语句。(2)当super调用被隐藏的方法时,该方法中出现的成员变量是被子类隐藏的成员变量 或继承的成员变量。5.实验后的练习:参照建设银行或大连银行,在编写一个商业银行,让程序输出 8000元存在商业银行8年零236天的利息。/Ban k.javapublic class Bankint savedM on ey;int year;double in terest;double in terestRate=0.29;public double computerI nterest()in terest=year*i nterestRate*savedM on ey;return in ter
18、est;public void setI nterestRate( double rate)in terestRate=rate;/ CommercialBa nkpublic class CommercialBa nk exte nds Bank double year;public double computerI nterest()super.year=(i nt)year;double r=year-(i nt)year;int day=(i nt)(r*1000);double yearI nterest=puterl nterest();double day In terest=d
19、ay*0.00012*savedMo ney;in terest=yearI nterest+day In terest;System.out.printf(%d 元存在商业银行 %d 年零 %d 天的利息:f 元n ,savedM on ey,super.year,day,i nterest);return in terest;/ SaveMon ey.javapublic class SaveMon eypublic static void main(String args)int amou nt=8000;CommercialBa nk bank=new CommercialBa nk(
20、);ban k.savedM on ey=am ount;ban k.year=8.236;ban k.set In terestRate(0.035);double in terest=ba puterl nterest();实验3:公司支出的总薪水1实验要求:要求有一个 abstract类,类名为 Employee。Employee 的子类有 YearWorker、MonthWorker、 WeekWorker。YearWorker对象按年领取薪水, MonthWorker按月领取薪水、 WeekWorker按 周领取的薪水。 Employee类有一个abstract方法:public a
21、bstract earnin gs();子类必须重写父类的 earin gs()方法,给出各自领取报酬的具体方式。有一个Company类,该类用 Employee对象数组作为成员, Employee对象数组的单元可以 是YearWorker对象的上转型对象、 MonthWorker独享的上转型独享或 weekworker对象的上 转型独享。程序能输出 Company对象一年需要支付的薪水总额。2实验代码:abstract class Employeepublic abstract double earnin gs();class YearWorker exte nds Employeepubl
22、ic double earnin gs()return 12000;class Mon thWorker exte nds Employeepublic double earnin gs()return 12*2300;class WeekWorker exte nds Employeepublic double earnin gs()return 52*780;class Compa nyEmployee employee;double salaries=0;Compa ny (Employee employee)this.employee=employee;public double sa
23、lariesPay()salaries=0;for(i nt i=0;iemployeeen gth;i+) salaries=salaries+employeei.earnin gs();retur n salaries;public class Compa ny Salarypublic static void main(String args)Employee employee=new Employee29;for(i nt i=0;iemployee .len gth;i+)if(i%3=0)employeei=new WeekWorker();else if(i%3=1)employ
24、eei=n ew Mon thWorker();else if(i%3=2)employeei=new YearWorker();Compa ny compa ny=new Compa ny (employee);System.out.println(公司薪水总额:+company.salariesPay()+元); 3.实验结果:恪司 薪 人总额:阳帕财血:正 lPi*ess an! keys tzu cant inue -*14实验分析:尽管abstract类不能创建对象,但 abstract类声明的对象可以存放子类对象的引用,即成为子类对象的上转型对象。5实验后的练习:(1)子类year
25、worker如果不重写earnings ()方法,程序编译是提示怎么样的错误。YearWorker不是抽象的,并且未覆盖 Employee中的抽象方法 earnings()class YearWorker exte nds Employee(2)在增加一种雇员,并计算公司一年的总薪水。class DayWorker exte nds Employeepublic double earnin gs()return 365*100;将for语句改写为:for(i nt i=0;iemployeeen gth;i+)if(i%4=0)employee=new WeekWorker();else if(i%4=1)employeei=n ew Mon thWorker();else if(i%4=2) employeei=new YearWorker();else if(i%4=3)employeei=new DayWorker();
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1