java继承与接口实验三.docx

上传人:b****8 文档编号:11906772 上传时间:2023-06-03 格式:DOCX 页数:12 大小:79.78KB
下载 相关 举报
java继承与接口实验三.docx_第1页
第1页 / 共12页
java继承与接口实验三.docx_第2页
第2页 / 共12页
java继承与接口实验三.docx_第3页
第3页 / 共12页
java继承与接口实验三.docx_第4页
第4页 / 共12页
java继承与接口实验三.docx_第5页
第5页 / 共12页
java继承与接口实验三.docx_第6页
第6页 / 共12页
java继承与接口实验三.docx_第7页
第7页 / 共12页
java继承与接口实验三.docx_第8页
第8页 / 共12页
java继承与接口实验三.docx_第9页
第9页 / 共12页
java继承与接口实验三.docx_第10页
第10页 / 共12页
java继承与接口实验三.docx_第11页
第11页 / 共12页
java继承与接口实验三.docx_第12页
第12页 / 共12页
亲,该文档总共12页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

java继承与接口实验三.docx

《java继承与接口实验三.docx》由会员分享,可在线阅读,更多相关《java继承与接口实验三.docx(12页珍藏版)》请在冰点文库上搜索。

java继承与接口实验三.docx

java继承与接口实验三

信息与计算科学专业实验报告

课程名称

Java课程设计

总实验学时:

16

第3次

共6次

实验项目名称

继承与接口

本次实验学时数:

3

实验类型

验证

日期

2012年3月6日星期二

年级

学生姓名

学号

课任教师

1.实验目的

巩固如下概念:

子类的继承性、子类对象的创建过程、成员变量的继承与隐藏、方法的继承与重写;掌握上转型对象的使用;掌握接口回调技术。

2.实验要求

实验前,应事先熟悉相关知识点,拟出相应的实验操作步骤,明确实验目的和要求;实验过程中,服从实验指导教师安排,遵守实验室的各项规章制度,爱护实验仪器设备;实验操作完成后,认真书写实验报告,总结实验经验,分析实验过程中出现的问题。

3.实验内容

1、继承

编写一个Java应用程序,除主类外,该程序中还有4个类:

People、ChinaPeople、

AmericanPeople和BeijingPeople类。

要求如下:

People类有访问权限是protected的double型成员变量:

height和weight,以及publicvoidspeakHello()、publicvoidaverageHeight()、publicvoidaverageWeight()方法;ChinaPeople类是People的子类,新增了publicvoidchinaGongfu()方法。

要求ChinaPeople重写父类的publicvoidspeakHello()、publicvoidaverageHeight()、publicvoidaverageWeight()方法;AmericanPeople类是People的子类,新增publicvoidamericanBoxing()方法。

要求AmericanPeople重写父类的publicvoidspeakHello()、publicvoidaverageHeight()、publicvoidaverageWeight()方法;BeijingPeople类是ChinaPeople的子类,新增publicvoidbeijingOpera()方法。

要求BeijingPeople重写父类的publicvoidspeakHello()、publicvoidaverageHeight()、publicvoidaverageWeight()方法;请按模板要求,将【代码】替换为Java程序代码。

2、上转型对象

编写一个Java应用程序,要求有一个abstract类,类名为Employee。

Employee的子类有YearWorker、MonthWorker和WeekWorker。

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

Employee类有一个abstract方法:

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

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

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

3、接口回调

卡车要装载一批货物,货物有3种商品:

电视、计算机和洗衣机。

需要计算出大货车和小货车各自所装载的3中货物的总重量。

编写一个Java应用程序,要求有一个ComputeWeight接口,该接口中有一个方法:

publicdoublecomputeWeight();有3个实现该接口的类:

Television、Computer和WashMachine。

这3个类通过实现接口ComputeWeight给出自重。

有一个Car类,该类用ComputeWeight接口类型的数组作为成员,那么该数组的元素就可以存放Television对象的引用、Computer对象的引用或WashMachine对象的引用。

程序能输出Car对象所装载的货物的总重量。

4.实验步骤、实施过程、关键代码、实验结果及分析说明等

(1)代码:

classPeople

{

protecteddoubleweight,height;

publicvoidspeakHello()

{

System.out.println("yayawawa");

}

publicvoidaverageHeight()

{

height=173;

System.out.println("averageheight:

"+height);

}

publicvoidaverageWeight()

{

weight=70;

System.out.println("averageweight:

"+weight);

}

}

classChinaPeopleextendsPeople

{

publicvoidspeakHello()

{

System.out.println("你好,睡了吗?

");

}//【代码1】重写publicvoidspeakHello()方法

publicvoidaverageHeight()

{

System.out.println("中国人的平均身高:

168.78厘米");

}//【代码2】重写publicvoidaverageHeight()方法

publicvoidaverageWeight()

{

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

65公斤");

}//【代码3】重写publicvoidaverageWeight()方法

publicvoidchinaGongfu()

{

System.out.println("坐如钟,站如松,睡如弓.");//【代码4】输出中国武术的信息

}

}

classAmericanPeopleextendsPeople

{

publicvoidspeakHello()

{

System.out.println("Howdoyoudo");

}//【代码5】重写publicvoidspeakHello()方法

publicvoidaverageHeight()

{

System.out.println("美国人平均身高为175厘米");

}//【代码6】重写publicvoidaverageHeight()方法

publicvoidaverageWeight()

{

System.out.println("美国人平均体重为75公斤");

}//【代码7】重写publicvoidaverageWeight()方法

publicvoidamericanBoxing()

{

System.out.println("直拳、钩拳.");//【代码8】输出拳击的信息

}

}

classBeijingPeopleextendsChinaPeople

{

publicvoidspeakHello()

{

System.out.println("你好!

");

}//【代码9】重写publicvoidspeakHello()方法

publicvoidaverageHeight()

{

System.out.println("北京人平均身高为170厘米");

}//【代码10】重写publicvoidaverageHeight()方法

publicvoidaverageWeight()

{

System.out.println("北京人平均体重为65公斤");

}//【代码11】重写publicvoidaverageWeight()方法

publicvoidbeijingOpera()

{

System.out.println("京剧的形成大约有150年左右.");//【代码12】输出京剧的信息

}

}

publicclassExample3.1

{

publicstaticvoidmain(Stringargs[])

{

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();

}

}实验结果:

(2)代码:

abstractclassEmployee

{

doublesalary;

publicabstractdoubleearnings();//返回年收入

}

classYearWorkerextendsEmployee

{

publicdoubleearnings()

{

return(4000);

}//重写earnings()方法

}

classMonthWorkerextendsEmployee

{

publicdoubleearnings()

{

return(3000);

}//重写earnings()方法。

}

classWeekWorkerextendsEmployee

{

publicdoubleearnings()

{

return(2000);

}//重写earnings()方法。

}

classCompany

{

Employee[]employee;

doublesalaries=0;

Company(Employee[]employee)

{

this.employee=employee;

}

publicdoublesalariesPay()

{

salaries=0;

for(inti=0;i

salaries=salaries+employee[i].earnings();

//计算salaries。

returnsalaries;

}

}

publicclassHardWork

{

publicstaticvoidmain(Stringargs[])

{

Employee[]employee=newEmployee[20];

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)代码:

interfaceComputerWeight

{

publicdoublecomputerWeight();

}

classTelevisionimplementsComputerWeight

{

publicdoublecomputerWeight()

{

XX文库-让每个人平等地提升自我return155;

}

}

classComputerimplementsComputerWeight

{

publicdoublecomputerWeight()

{

return105;

}

}

classWashMachineimplementsComputerWeight

{

publicdoublecomputerWeight()

{

return80;

}

}

classCar

{

ComputerWeight[]goods;

doubletotalWeights=0;

Car(ComputerWeight[]goods)

{this.goods=goods;

}

publicdoublegetTotalWeights()

{

totalWeights=0;

for(inti=0;i

{

totalWeights+=goods[i].computerWeight();

}

returntotalWeights;

}

}

publicclassRoad

{

publicstaticvoidmain(Stringargs[])

{

ComputerWeight[]goodsOne=newComputerWeight[50],

goodsTwo=newComputerWeight[22];

for(inti=0;i

{

if(i%3==0)

goodsOne[i]=newTelevision();

elseif(i%3==1)

goodsOne[i]=newComputer();

elseif(i%3==2)

goodsOne[i]=newWashMachine();

}

for(inti=0;i

{

if(i%3==0)

goodsTwo[i]=newTelevision();

elseif(i%3==1)

goodsTwo[i]=newComputer();

elseif(i%3==2)

goodsTwo[i]=newWashMachine();

}

Carbigcar=newCar(goodsOne);

System.out.println("大货车装载的货物重量:

"+bigcar.getTotalWeights());

Carsmallcar=newCar(goodsTwo);

System.out.println("小货车装载的货物重量:

"+smallcar.getTotalWeights());

}

}

实验结果:

5.小结、体会或建议:

JAVA不支持多重继承,子类只能有一个父类。

类声明中,用关键字extends来声明一个类是另外一个类的子类。

子类和父类在同一个包中,子类自然继承了其父类中不是private的成员变量和方法作为自己的,访问权限不变。

不在同一个包中,子类继承父类的protecded,public成员变量和方法,子类不能继承父类的友好变量和方法。

学生签名:

2012年3月6日

6.教师评语:

程序基本正确,基本完成实验的要求;实验报告格式较规范。

教师签名:

2012年3月22日

成绩

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

当前位置:首页 > 经管营销 > 经济市场

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

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