(推荐)接口与实现接口的类

上传人:日度 文档编号:131976459 上传时间:2020-05-11 格式:DOC 页数:14 大小:85KB
返回 下载 相关 举报
(推荐)接口与实现接口的类_第1页
第1页 / 共14页
(推荐)接口与实现接口的类_第2页
第2页 / 共14页
(推荐)接口与实现接口的类_第3页
第3页 / 共14页
(推荐)接口与实现接口的类_第4页
第4页 / 共14页
(推荐)接口与实现接口的类_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《(推荐)接口与实现接口的类》由会员分享,可在线阅读,更多相关《(推荐)接口与实现接口的类(14页珍藏版)》请在金锄头文库上搜索。

1、 一、实验目的和要求1理解接口的作用,理解接口和实现接口的类的关系2 掌握声明接口,一个类实现多个接口的声明和使用方法3 理解内嵌类型的概念,掌握声明内部类的方法 二、实验环境Windows2000/Windows XP,JDK 1.21.6 三、实验内容和步骤 实验1 评价成绩1. 实验要求体操比赛计算选手成绩的办法是去掉一个最高分和最低分后再计算平均分,而学校考察一个班级的某科目的考试情况时,是计算全班同学的平均成绩。Gymmastics类和School类都实现了ComputerAverage接口,但实现的方式不同。2. 程序模板Estimatior.java interface Comp

2、urerAverage public double average(double x);class Gymnastics implements CompurerAverage public double average(double x) int count=x.length; double aver=0,temp=0; for(int i=0;icount;i+) for(int j=i;jcount;j+) if(xjxi) temp=xj; xj=xi; xi=temp; for(int i=1;i2) aver=aver/(count-2); else aver=0; return a

3、ver; class School implements CompurerAverage /重写public double average(double x);返回数组x的元素的算术平均 【代码1】/重写public double average(double x)方法,返回数组x的元素的算术平均public class Estimator public static void main(String args) double a = 9.89,9.88,9.99,9.12,9.69,9.76,8.97; double b =89,56,78,90,100,77,56,45,36,79,98;

4、 CompurerAverage computer; computer=new Gymnastics(); double result= 【代码2】 /computer调用average(double x)方法,将数组a传递给参数x System.out.printf(%n); System.out.printf(体操选手最后得分:%5.3fn,result); computer=new School(); result=【代码3】 /computer调用average(double x)方法,将数组b传递给参数x System.out.printf(班级考试平均分数:%-5.2f,resul

5、t); 实验结果【代码1】public double average(double x) double aver=0; for(int i=0;ix.length;i+) aver+=xi; aver/=x.length; return aver; 【代码2】computer.average(a);【代码3】computer.average(b);3. 实验指导l 可以把实现某一接口的类创建的对象的引用赋给该接口声明的接口变量中,那么该接口变量就可以调用被类实现的接口方法。l 接口产生的多态就是指不同类在实现同一个接口时可能具有不同的实现方式。实验2 货车的装载量 1.实验要求 货车要装载一批

6、货物,货物由三种商品组成:电视、计算机和洗衣机。卡车需要计算出整批货物的重量。 要求有一个ComputerWeight接口,该接口中有一个方法: public double computer Weight( ) 有三个实现该接口的类:Television、Computer和WashMachine。这三个类通过实现接口computerTotalSales给出自重。 有一个Truck类,该类用ComputerWeight接口类型的数组作为成员(Truck类面向接口),那么该数组的单元就可以存放Television对象的引用、Computer对象的引用或WashMachine对象的引用。程序能输出T

7、ruck 对象所装载的货物的总重量。2.程序模板 CheckCarWeight.javainterface ComputerWeight public double computeWeight();class Television implements ComputerWeight 【代码1】 /重写computeWeight()方法class Computer implements ComputerWeight 【代码2】 /重写computeWeight()方法 class WashMachine implements ComputerWeight 【代码3】 /重写computeWeig

8、ht()方法class Truck ComputerWeight goods; double totalWeights=0; Truck(ComputerWeight goods) this.goods=goods; public void setGoods(ComputerWeight goods) this.goods=goods; public double getTotalWeights() totalWeights=0; 【代码4】 /计算totalWeights return totalWeights; public class CheckCarWeight public stat

9、ic void main(String args) ComputerWeight goods=new ComputerWeight650; /650件货物 for(int i=0;igoods.length;i+) /简单分成三类 if(i%3=0) goodsi=new Television(); else if(i%3=1) goodsi=new Computer(); else if(i%3=2) goodsi=new WashMachine(); Truck truck=new Truck(goods); System.out.printf(n货车装载的货物重量:%-8.5f kgn,

10、truck.getTotalWeights(); goods=new ComputerWeight68; /68件货物 for(int i=0;igoods.length;i+) /简单分成两类 if(i%2=0) goodsi=new Television(); else goodsi=new WashMachine(); truck.setGoods(goods); System.out.printf(货车装载的货物重量:%-8.5f kgn,truck.getTotalWeights(); 实验结果【代码1】public double computeWeight() return 40.

11、0; 【代码2】public double computeWeight() return 50.0; 【代码3】public double computeWeight() return 60.0; 【代码4】for(int i=0;igoods.length;i+) totalWeights=totalWeights+puteWeight();3.实验指导l 对于【代码1】,可以简单返回一个值表示货物的重量,比如: public double computerWeight( ) returen 3.5l 对于数组goods的每个单元存放的是实现ComputerWeight接口的对象的引用,实验

12、中的【代码4】通过循环语句让数组的每个单元调用computerWeight()方法,并将该方法返回的值累加到totalWeight,如下所示:for(int i=0; igoods.length;i+) totalWeight += puterWeight( );4.扩展练习在实验的基础上编写一个实现ComputerWeight接口的类,比如Refrigerator,这样一来,货车装载的货物中就可以有Refrigerator类型的对象。当系统增加一个实现ComputerWeight接口的类后,Truck类需要进行修改吗?不需要修改代码:interface ComputerWeight public double computeWeight();class Television implements ComputerWeight public double computeWeight() return 40.0; /重写computeWeight()方法class Computer implements ComputerWeight public double computeWeight()

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 医学/心理学 > 基础医学

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号