第八次实验报告(共11页)_第1页
第八次实验报告(共11页)_第2页
第八次实验报告(共11页)_第3页
第八次实验报告(共11页)_第4页
第八次实验报告(共11页)_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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的子类,新增了pu

2、blic void averageHeight()和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.实验代码:/Peop

3、le.javapublic class People protected 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:&quo

4、t;+weight);/ChinaPeople.javapublic class ChinaPeople extends 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("中国人

5、的平均体重:"+weight+"千克");public void chinaGongfu() System.out.println("坐如钟,站如松,睡如弓");/AmericanPeople.javapublic class AmericanPeople extends People public void speakHello () System.out.println("How do you do");public void averageHeight() height=176;System.out.println(&

6、quot;American's average height:"+height+"厘米");public void averageWeight() weight=75;System.out.println("American's average weight:"+weight+" kg");public void americanBoxing() System.out.println("直拳,勾拳,组合拳");/BeijingPeople.javapublic class BeijingP

7、eople 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("花脸、青衣、花旦和老生&quo

8、t;);/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();china

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

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

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

12、bank和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;/ ConstructionB

13、ank.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=puterInterest();double dayInterest=day*0.0001*savedMoney;interest=yearInterest+dayInterest;System.out.printf("%d元存

14、在建设银行%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=puterInterest(); double day

15、Interest=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 Constr

16、uctionBank();bank1.savedMoney=amount;bank1.year=8.236;bank1.setInterestRate(0.035);double interest1=puterInterest();BankOfDalian bank2=new BankOfDalian();bank2.savedMoney=amount;bank2.year=8.236;bank2.setInterestRate(0.035);double interest2=puterInterest();System.out.printf("两个银行利息相差%f元n",

17、interest2-interest1);3.实验结果:4.实验分析:(1) 子类不继承父类的构造方法,因此子类在其构造方法中需使用super来调用父类的构造方法,并且super必须是子类构造方法中的头一条语句。(2) 当super调用被隐藏的方法时,该方法中出现的成员变量是被子类隐藏的成员变量或继承的成员变量。5.实验后的练习:参照建设银行或大连银行,在编写一个商业银行,让程序输出8000元存在商业银行8年零236天的利息。/Bank.javapublic class Bankint savedMoney;int year;double interest;double interestRat

18、e=0.29;public double computerInterest()interest=year*interestRate*savedMoney;return interest;public void setInterestRate( double rate)interestRate=rate;/ CommercialBankpublic class CommercialBank extends Bank double year; public double computerInterest() super.year=(int)year; double r=year-(int)year

19、; int day=(int)(r*1000); double yearInterest=puterInterest(); double dayInterest=day*0.00012*savedMoney; interest=yearInterest+dayInterest; System.out.printf("%d元存在商业银行%d年零%d天的利息:%f元n",savedMoney,super.year,day,interest); return interest; / SaveMoney.java public class SaveMoneypublic stati

20、c void main(String args)int amount=8000;CommercialBank bank=new CommercialBank();bank.savedMoney=amount;bank.year=8.236;bank.setInterestRate(0.035);double interest=puterInterest();实验3:公司支出的总薪水1.实验要求:要求有一个abstract类,类名为Employee。Employee的子类有YearWorker、MonthWorker、WeekWorker。YearWorker对象按年领取薪水,MonthWork

21、er按月领取薪水、WeekWorker按周领取的薪水。Employee类有一个abstract方法:public abstract earnings();子类必须重写父类的earings()方法,给出各自领取报酬的具体方式。有一个Company类,该类用Employee对象数组作为成员,Employee对象数组的单元可以是YearWorker对象的上转型对象、MonthWorker独享的上转型独享或weekworker对象的上转型独享。程序能输出Company对象一年需要支付的薪水总额。2.实验代码:abstract class Employeepublic abstract double e

22、arnings();class YearWorker extends Employeepublic double earnings()return 12000;class MonthWorker extends Employeepublic double earnings()return 12*2300;class WeekWorker extends Employeepublic double earnings()return 52*780;class CompanyEmployee employee;double salaries=0;Company(Employee employee)this.employee=employee;public double salariesPay()salaries=0;for(int i=0;i<employee.length;i+)salaries=salaries+employeei.earnings();return salaries;public class CompanySalarypublic static void main(String args)Employee employee=new Employee29;for(int i=0;i<employee.

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论