第八次实验报告.docx

上传人:b****8 文档编号:9190918 上传时间:2023-05-17 格式:DOCX 页数:15 大小:121.61KB
下载 相关 举报
第八次实验报告.docx_第1页
第1页 / 共15页
第八次实验报告.docx_第2页
第2页 / 共15页
第八次实验报告.docx_第3页
第3页 / 共15页
第八次实验报告.docx_第4页
第4页 / 共15页
第八次实验报告.docx_第5页
第5页 / 共15页
第八次实验报告.docx_第6页
第6页 / 共15页
第八次实验报告.docx_第7页
第7页 / 共15页
第八次实验报告.docx_第8页
第8页 / 共15页
第八次实验报告.docx_第9页
第9页 / 共15页
第八次实验报告.docx_第10页
第10页 / 共15页
第八次实验报告.docx_第11页
第11页 / 共15页
第八次实验报告.docx_第12页
第12页 / 共15页
第八次实验报告.docx_第13页
第13页 / 共15页
第八次实验报告.docx_第14页
第14页 / 共15页
第八次实验报告.docx_第15页
第15页 / 共15页
亲,该文档总共15页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

第八次实验报告.docx

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

第八次实验报告.docx

第八次实验报告

第八次实验

实验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()、publicvoid

averageHeight()禾口publicvoidaverageWeight()方法。

(4)BeijingPeople类是ChinaPeople的子类,新增publicvoidbeijingOpera()方

法。

2•实验代码:

//People.java

publicclassPeople{

protecteddoubleweight,height;

publicvoidspeakHello。

{

System.out.println("yayayaya");

}

publicvoidaverageHeight(){

height=173;System.out.println("averageheight:

"+height);

}

publicvoidaverageWeight(){

weight=70;System.out.println("averageweight:

"+weight);

}

}

//ChinaPeople.java

publicclassChinaPeopleextendsPeople{

publicvoidspeakHello(){

System.out.println(”您好");

}

publicvoidaverageHeight(){

height=168.78;System.out.println(”中国人的平均身高:

"+height+"厘米");

}

publicvoidaverageWeight(){

weight=65;

System.out.println(”中国人的平均体重:

"+weight+"千克”);

}

publicvoidchinaGongfu(){System.out.println(”坐如钟,站如松,睡如弓”);

}

}

//AmericanPeople.java

publicclassAmericanPeopleextendsPeople{

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.java

publicclassBeijingPeopleextendsChinaPeople{

publicvoidaverageHeight(){

height=172.5;

System.out.println(”北京人的平均身高:

"+height+"厘米");

}

publicvoidaverageWeight(){

weight=70;

System.out.println("北京人得平均体重:

"+weight+"千克");

}

publicvoidbeijingOpera(){

System.out.println(”花脸、青衣、花旦和老生”);

}

}

//Example.java

publicclassExample{

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)方法重写时要保证方法的名字、类型、参数的个数和类型同父类的某个方法完全想同。

这样,子类继承的方法才能被隐藏。

⑵子类在重写方法时,如果重写的方法是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.java

publicclassBank{

intsavedMoney;

intyear;

doubleinterest;

doubleinterestRate=0.29;

publicdoublecomputerInterest(){

interest=year*interestRate*savedMoney;

returninterest;

}

publicvoidsetInterestRate(doublerate){

interestRate=rate;

}

}

//ConstructionBank.java

publicclassConstructionBankextendsBank{

doubleyear;

publicdoublecomputerInterest(){

super.year=(int)year;

doubler=year-(int)year;

intday=(int)(r*1000);

doubleyearInterest=puterlnterest();

doubledayInterest=day*0.0001*savedMoney;

interest=yearInterest+dayInterest;

System.out.printf("%d元存在建设银行%d年零%d天的禾U息:

%f元

\n",savedMoney,super.year,day,interest);

returninterest;

}

}

//BankOfDalian.java

publicclassBankOfDalianextendsBank{

doubleyear;

publicdoublecomputerInterest(){

super.year=(int)year;

doubler=year-(int)year;

intday=(int)(r*1000);

doubleyearInterest=puterlnterest();

doubledayInterest=day*0.00012*savedMoney;

interest=yearInterest+dayInterest;

System.out.printf("%d元存在大连银行%d年零%d天的禾U息:

%f元\n",savedMoney,super.year,day,interest);

returninterest;

//SaveMoney.java

publicclassSaveMoney{

publicstaticvoidmain(Stringargs[]){

intamount=8000;

ConstructionBankbank1=newConstructionBank();

bank1.savedMoney=amount;

bank1.year=8.236;

bank1.setInterestRate(0.035);

doubleinterest1=baputerlnterest();

BankOfDalianbank2=newBankOfDalian();

bank2.savedMoney=amount;

bank2.year=8.236;

bank2.setInterestRate(0.035);

doubleinterest2=baputerlnterest();

System.out.printf("两个银行利息相差%f元\n”,interest2-interest1);}

}

3.实验结果:

4.实验分析:

(1)子类不继承父类的构造方法,因此子类在其构造方法中需使用super来调用父类的

构造方法,并且super必须是子类构造方法中的头一条语句。

(2)当super调用被隐藏的方法时,该方法中出现的成员变量是被子类隐藏的成员变量或继承的成员变量。

5.实验后的练习:

参照建设银行或大连银行,在编写一个商业银行,让程序输出8000元存在商业银行8年零

236天的利息。

//Bank.java

publicclassBank{

intsavedMoney;

intyear;

doubleinterest;

doubleinterestRate=0.29;

publicdoublecomputerInterest(){

interest=year*interestRate*savedMoney;

returninterest;

}

publicvoidsetInterestRate(doublerate){

interestRate=rate;

}

}

//CommercialBank

publicclassCommercialBankextendsBank{

doubleyear;

publicdoublecomputerInterest(){

super.year=(int)year;

doubler=year-(int)year;

intday=(int)(r*1000);

doubleyearInterest=puterlnterest();

doubledayInterest=day*0.00012*savedMoney;

interest=yearInterest+dayInterest;

System.out.printf("%d元存在商业银行%d年零%d天的利息:

%f元

\n",savedMoney,super.year,day,interest);

returninterest;

}

}

//SaveMoney.java

publicclassSaveMoney{

publicstaticvoidmain(Stringargs[]){

intamount=8000;

CommercialBankbank=newCommercialBank();

bank.savedMoney=amount;

bank.year=8.236;

bank.setInterestRate(0.035);

doubleinterest=baputerlnterest();

实验3:

公司支出的总薪水

1•实验要求:

要求有一个abstract类,类名为Employee。

Employee的子类有YearWorker、MonthWorker、WeekWorker。

YearWorker对象按年领取薪水,MonthWorker按月领取薪水、WeekWorker按周领取的薪水。

Employee类有一个abstract方法:

publicabstractearnings();

子类必须重写父类的earings()方法,给出各自领取报酬的具体方式。

有一个Company类,该类用Employee对象数组作为成员,Employee对象数组的单元可以是YearWorker对象的上转型对象、MonthWorker独享的上转型独享或weekworker对象的上转型独享。

程序能输出Company对象一年需要支付的薪水总额。

2•实验代码:

abstractclassEmployee{

publicabstractdoubleearnings();

}

classYearWorkerextendsEmployee{

publicdoubleearnings(){

return12000;

}

}

classMonthWorkerextendsEmployee{

publicdoubleearnings(){

return12*2300;

classWeekWorkerextendsEmployee{

publicdoubleearnings(){

return52*780;

}

}

classCompany{

Employee]]employee;

doublesalaries=0;

Company(Employee]]employee){

this.employee=employee;

}

publicdoublesalariesPay(){

salaries=0;

for(inti=0;i

}

returnsalaries;

}

}

publicclassCompanySalary{

publicstaticvoidmain(Stringargs[]){

Employee]]employee=newEmployee[29];

for(inti=0;i

if(i%3==0)

employee[i]=newWeekWorker();

elseif(i%3==1)

employee[i]=newMonthWorker();

elseif(i%3==2)

employee[i]=newYearWorker();

}

Companycompany=newCompany(employee);

System.out.println("公司薪水总额:

"+company.salariesPay()+"元");}

}

3.实验结果:

恪司薪人总额:

阳帕财血:

正lPi*essan^!

keystzucantinue--

*1

4•实验分析:

尽管abstract类不能创建对象,但abstract类声明的对象可以存放子类对象的引用,即成

为子类对象的上转型对象。

5•实验后的练习:

(1)子类yearworker如果不重写earnings()方法,程序编译是提示怎么样的错误。

YearWorker不是抽象的,并且未覆盖Employee中的抽象方法earnings()

classYearWorkerextendsEmployee{

(2)在增加一种雇员,并计算公司一年的总薪水。

classDayWorkerextendsEmployee{

publicdoubleearnings(){

return365*100;

}

}

将for语句改写为:

for(inti=0;i

if(i%4==0)

employee"]=newWeekWorker();

elseif(i%4==1)

employee[i]=newMonthWorker();

elseif(i%4==2)employee[i]=newYearWorker();

elseif(i%4==3)

employee[i]=newDayWorker();

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > IT计算机 > 电脑基础知识

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2