接口与实现接口的类 java程序设计.doc_第1页
接口与实现接口的类 java程序设计.doc_第2页
接口与实现接口的类 java程序设计.doc_第3页
接口与实现接口的类 java程序设计.doc_第4页
接口与实现接口的类 java程序设计.doc_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

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

文档简介

华北电力大学实 验 报 告| 实验名称 接口与实现接口的类 课程名称 Java程序设计 | 专业班级:信管1301 学生姓名:王雯敏 学 号:201306040121 成 绩:指导教师:张学斌 实验日期: 2015.4.20华 北 电 力 大 学 实 验 报 告一、实验目的和要求1理解接口的作用,理解接口和实现接口的类的关系2 掌握声明接口,一个类实现多个接口的声明和使用方法3 理解内嵌类型的概念,掌握声明内部类的方法 二、实验环境Windows2000/Windows XP,JDK 1.21.6 三、实验内容和步骤 实验1 评价成绩1. 实验要求体操比赛计算选手成绩的办法是去掉一个最高分和最低分后再计算平均分,而学校考察一个班级的某科目的考试情况时,是计算全班同学的平均成绩。Gymmastics类和School类都实现了ComputerAverage接口,但实现的方式不同。2. 程序模板Estimatior.java interface CompurerAverage 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 aver; 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; 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,result); 实验结果【代码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.实验要求 货车要装载一批货物,货物由三种商品组成:电视、计算机和洗衣机。卡车需要计算出整批货物的重量。 要求有一个ComputerWeight接口,该接口中有一个方法: public double computer Weight( ) 有三个实现该接口的类:Television、Computer和WashMachine。这三个类通过实现接口computerTotalSales给出自重。 有一个Truck类,该类用ComputerWeight接口类型的数组作为成员(Truck类面向接口),那么该数组的单元就可以存放Television对象的引用、Computer对象的引用或WashMachine对象的引用。程序能输出Truck 对象所装载的货物的总重量。2.程序模板 CheckCarWeight.javainterface ComputerWeight public double computeWeight();class Television implements ComputerWeight 【代码1】 /重写computeWeight()方法class Computer implements ComputerWeight 【代码2】 /重写computeWeight()方法 class WashMachine implements ComputerWeight 【代码3】 /重写computeWeight()方法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 static 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,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.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接口的对象的引用,实验中的【代码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() return 50.0; /重写computeWeight()方法 class WashMachine implements ComputerWeight public double computeWeight() return 60.0; /重写computeWeight()方法class Refrigerator implements ComputerWeight public double computeWeight() return 45.0; /重写computeWeight()方法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; for(int i=0;igoods.length;i+) totalWeights=totalWeights+puteWeight();/计算totalWeights return totalWeights; public class CheckCarWeight public static void main(String args) ComputerWeight goods=new ComputerWeight650; /650件货物 for(int i=0;igoods.length;i+) /简单分成三类 if(i%4=0) goodsi=new Television(); else if(i%4=1) goodsi=new Computer(); else if(i%4=2) goodsi=new WashMachine(); else if(i%4=3) goodsi=new Refrigerator(); Truck truck=new Truck(goods); System.out.printf(n货车装载的货物重量:%-8.5f kgn,truck.getTotalWeights(); 截图实验3 小狗的状态1 实验要求小狗崽不同环境下可能呈现不同的状态表现,要求用接口封装小狗的状态。具体要求如下: (1)编写一个接口DogState,该接口有一个名字为void showState()的方法。 (2)编写Dog类,该类中有一个DogState接口声明的变量state。另外,该类有一个show()方法,在该方法中让接口state回调showState()方法。 (3)编写若干个实现DogState接口的类,负责刻画小狗的各种状态。 (4)编写主类,在主类中测试小狗的各种状态。2.程序模板 interface DogState public void showState();class SoftlyState implements DogState public void showState() System.out.println(听主人的命令); class MeetEnemyState implements DogState public void showState() System.out.println(狂叫,并冲向去很咬敌人); /【代码1】 /public void showState() class MeetFriendState implements DogState public void showState() System.out.println(晃动尾巴,表示欢迎); /【代码2】 /重写public void showState()方法class MeetAnotherDog implements DogState public void showState() System.out.println(嬉戏); /【代码3】 /重写public void showState()方法class Dog DogState state; public void show() state.showState(); public void setState(DogState s) state = s; public class CheckDogState public static void main(String args) Dog yellowDog =new Dog(); System.out.print(狗在主人面前:); yellowDog.setState(new SoftlyState(); yellowDog.show(); System.out.print(狗遇到敌人:); yellowDog.setState(new MeetEnemyState(); yellowDog.show(); System.out.print(狗遇到朋友:); yellowDog.setState(new MeetFriendState(); yellowDog.show(); System.out.print(狗遇到同伴:); yellowDog.setState(new MeetAnotherDog(); yellowDog.show(); 实验结果【代码1】public void showState() System.out.println(狂叫,并冲向去很咬敌人); 【代码2】 public void showState() System.out.println(晃动尾巴,表示欢迎); 【代码3】public void showState() System.out.println(嬉戏);3.实验扩展 用面向接口的思想编写程序,模拟水杯中的水在不同温度下可能出现的状态。代码interface WaterState public void showState();class UnderZero implements WaterState public void showState() System.out.println(固态); class Middle implements WaterState public void showState() System.out.println(液态); class BeyoundOneHundred implements WaterState public void showState() System.out.println(气态); class Water WaterState state; public void show() state.showState(); public void setState(WaterState s) state = s; public class CheckWaterState public static void main(String args) Water awater =new Water(); System.out.print(0度以下:); awater.setState(new UnderZero(); awater.show(); System.out.print(0到100度:); awater.setState(new Middle(); awater.show(); System.out.print(100度以上:); awater.setState(new BeyoundOneHundred(); awater.show(); 截图实验4 二维图形1.实验要求 声明椭圆类Ellipse,实现Area接口,计算椭圆面积2.程序结果实验结果代码interface Areapublic double computeArea(); public class Ellipse implements Areadouble r1,r2;Ellipse(double r1,double r2)this.r1=r1;this.r2=r2;public double computeArea()return r1*r2*Math.PI;public static void main(String args)Ellipse e=new Ellipse(2,4);System.out.print(椭圆面积为:+puteArea();截图实验5 三维物体1. 实验要求声明圆柱体类和圆锥类,继承矩形类Rectangle并实现Volume接口,计

温馨提示

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

评论

0/150

提交评论