JAVA第六次实验报告接口Word文件下载.docx

上传人:b****6 文档编号:18659755 上传时间:2022-12-31 格式:DOCX 页数:10 大小:76.72KB
下载 相关 举报
JAVA第六次实验报告接口Word文件下载.docx_第1页
第1页 / 共10页
JAVA第六次实验报告接口Word文件下载.docx_第2页
第2页 / 共10页
JAVA第六次实验报告接口Word文件下载.docx_第3页
第3页 / 共10页
JAVA第六次实验报告接口Word文件下载.docx_第4页
第4页 / 共10页
JAVA第六次实验报告接口Word文件下载.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

JAVA第六次实验报告接口Word文件下载.docx

《JAVA第六次实验报告接口Word文件下载.docx》由会员分享,可在线阅读,更多相关《JAVA第六次实验报告接口Word文件下载.docx(10页珍藏版)》请在冰豆网上搜索。

JAVA第六次实验报告接口Word文件下载.docx

i++){

for(intj=i;

j<

j++){

if(x[j]<

x[i]){

temp=x[i];

x[i]=x[j];

x[j]=temp;

}

}

}

for(inti=1;

count-1;

aver=aver+x[i];

if(count>

2)

aver=aver/(count-2);

else

aver=0;

returnaver;

}

classSchoolimplementsComputerAverage{

doubleaver=0;

必须重写该接口的所有方法。

3、实验练习

School类如果不重写publicdoubleaversge(doublex[])方法,程序编译时提示怎样的错误?

答:

SChool不是抽象的,并且未覆盖ComputerAverage中的抽象方法。

 

实验二

货车要装载一批货物,货物由三种商品组成:

电视、计算机和洗衣机,卡车需要计算出整批货物的重量。

2、实验代码

interfaceComputerWeight{

publicdoublecomputerWeight();

classTelevisionimplementsComputerWeight{

publicdoublecomputerWeight(){

return3.5;

classComputerimplementsComputerWeight{

return2.67;

classWashMachineimplementsComputerWeight{

return13.8;

classTruck{

ComputerWeight[]goods;

doubletotalWeights=0;

Truck(ComputerWeight[]goods){

this.goods=goods;

publicvoidsetGoods(ComputerWeight[]goods){

publicdoublegetTotalWeights(){

totalWeights=0;

goods.length;

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

returntotalWeights;

publicclassCheckCarWeight{

publicstaticvoidmain(Stringargs[]){

ComputerWeight[]goods=newComputerWeight[650];

//装载650件货物

i++){//分成三类

if(i%3==0)

goods[i]=newTelevision();

elseif(i%3==1)

goods[i]=newComputer();

elseif(i%3==2)

goods[i]=newWashMachine();

Trucktruck=newTruck(goods);

System.out.printf("

\n货车装载的货物重量:

%-8.5fkg\n"

truck.getTotalWeights());

goods=newComputerWeight[68];

//68件货物

i++){//分成两类

if(i%2==0)

else

truck.setGoods(goods);

货车装载的货物重量:

3、实验结果

4、实验分析

接口回调是指:

可以把使用某一接口的类型创建的对象引用赋给该接口声明的接口变量中,那么该接口变量就可以调用被实现的接口中的方法,当接口变量调用被类实现的接口中的方法时,就是通知相应的对象调用接口的方法,这一过程成为对象功能的接口回调。

接口的方法不一定相同,接口回调可能产生不同的行为。

5、实验练习

请在实验基础上再编写一个实现ComputerWeight接口的类,比如Refrigerrator。

这样一来,货车装载的货物中就可以有Refrigerrator类型的对象。

当系统增加一个实现ComputerWeight接口的类后,Truck类需要进行修改吗?

代码:

classRefrigerratorimplementsComputerWeight{

publicdoublecomputerWeight(){

return12.8;

实验三

小狗在不同环境条件下可能呈现不同的状态表现,要求接口封装小狗的状态。

具体要求如下:

(1)编写一个接口DogState,该接口有一个名为voidshowState()方法。

(2)编写一个Dog类,该类中有一个DogState接口声明的变量state,另外,该类有一个show()方法,在该方法中让接口state回调showState()方法。

(3)编写若干个实现DogState接口的类,负责刻画小狗的各种状态。

(4)编写主类,在主类中实现测试小狗的各种状态。

interfaceDogState{

publicvoidshowState();

classSoftlyStateimplementsDogState{

publicvoidshowState(){

System.out.println("

听主人的命令"

);

classMeetEnemyStateimplementsDogState{

狂叫,并冲过去狠咬敌人"

classMeetFriendStateimplementsDogState{

晃动尾巴,表示欢迎"

classMeetAnotherdogStateimplementsDogState{

嬉戏"

classDog{

DogStatestate;

publicvoidshow(){

state.showState();

publicvoidsetState(DogStates){

state=s;

publicclassCheckDogState{

DogyellowDog=newDog();

System.out.print("

狗在主人面前:

"

yellowDog.setState(newSoftlyState());

yellowDog.show();

狗遇到敌人:

yellowDog.setState(newMeetEnemyState());

狗遇到朋友:

yellowDog.setState(newMeetFriendState());

狗遇到同类:

yellowDog.setState(newMeetAnotherdogState());

面向接口编程是指当设计某种重要的类时,不让该类面向具体的类,而是面向接口,即所设计中的重要数据是接口声明的变量,而不是具体声明的对象。

用面向接口的思想编写一个程序,模拟水杯中的水在不同温度下可能出现的状态。

interfaceWaterState{

classSubzeroStateimplementsWaterState{

结冰"

classNormalStateimplementsWaterState{

冰冷或凉快"

classHotStateimplementsWaterState{

有热气冒出,温热"

classBoiledStateimplementsWaterState{

沸腾,烫"

classWater{

WaterStatestate;

publicvoidsetState(WaterStates){

publicclassCheckWaterState{

WatercupWater=newWater();

水杯中的水在零下时:

cupWater.setState(newSubzeroState());

cupWater.show();

水杯中的水在常温时:

cupWater.setState(newNormalState());

水杯中的水在六十七度时:

cupWater.setState(newHotState());

水杯中的水在一XX时:

cupWater.setState(newBoiledState());

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

当前位置:首页 > 高等教育 > 理学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1