1、i+) for(int j=i;jj+) if(xj2) aver=aver/(count-2); else aver=0; return aver; class School implements ComputerAverage double aver=0;必须重写该接口的所有方法。3、实验练习School类如果不重写public double aversge(double x)方法,程序编译时提示怎样的错误?答:SChool不是抽象的,并且未覆盖ComputerAverage中的抽象方法。实验二货车要装载一批货物,货物由三种商品组成:电视、计算机和洗衣机,卡车需要计算出整批货物的重量。2、
2、实验代码interface ComputerWeight public double computerWeight();class Television implements ComputerWeight public double computerWeight() return 3.5;class Computer implements ComputerWeight return 2.67;class WashMachine implements ComputerWeight return 13.8;class Truck ComputerWeight goods; double total
3、Weights=0; Truck(ComputerWeight goods) this.goods = goods; public void setGoods(ComputerWeight goods) public double getTotalWeights() totalWeights=0;goods.length; totalWeights = totalWeights + puterWeight(); return totalWeights;public class CheckCarWeight public static void main(String args) Compute
4、rWeight goods = new ComputerWeight650; /装载650件货物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 ComputerWeight
5、68; /68件货物i+) /分成两类 if(i%2 = 0) else truck.setGoods(goods);货车装载的货物重量:3、实验结果4、实验分析接口回调是指:可以把使用某一接口的类型创建的对象引用赋给该接口声明的接口变量中,那么该接口变量就可以调用被实现的接口中的方法,当接口变量调用被类实现的接口中的方法时,就是通知相应的对象调用接口的方法,这一过程成为对象功能的接口回调。接口的方法不一定相同,接口回调可能产生不同的行为。5、实验练习请在实验基础上再编写一个实现ComputerWeight接口的类,比如Refrigerrator。这样一来,货车装载的货物中就可以有Refrig
6、errator类型的对象。当系统增加一个实现ComputerWeight接口的类后,Truck类需要进行修改吗?代码:class Refrigerrator implements ComputerWeight public double computerWeight() return 12.8;实验三小狗在不同环境条件下可能呈现不同的状态表现,要求接口封装小狗的状态。具体要求如下:(1)编写一个接口DogState,该接口有一个名为void showState()方法。(2)编写一个Dog类,该类中有一个DogState接口声明的变量state,另外,该类有一个show()方法,在该方法中让接
7、口state回调 showState()方法。(3)编写若干个实现DogState接口的类,负责刻画小狗的各种状态。(4)编写主类,在主类中实现测试小狗的各种状态。interface DogState public void showState();class SoftlyState implements DogState public void showState() System.out.println(听主人的命令);class MeetEnemyState implements DogState狂叫,并冲过去狠咬敌人class MeetFriendState implements Do
8、gState晃动尾巴,表示欢迎class MeetAnotherdogState implements DogState嬉戏class Dog DogState state; public void show() state.showState(); public void setState(DogState s) state=s;public class CheckDogState Dog yellowDog=new Dog(); System.out.print(狗在主人面前: yellowDog.setState(new SoftlyState(); yellowDog.show();狗
9、遇到敌人: yellowDog.setState(new MeetEnemyState();狗遇到朋友: yellowDog.setState(new MeetFriendState();狗遇到同类: yellowDog.setState(new MeetAnotherdogState();面向接口编程是指当设计某种重要的类时,不让该类面向具体的类,而是面向接口,即所设计中的重要数据是接口声明的变量,而不是具体声明的对象。 用面向接口的思想编写一个程序,模拟水杯中的水在不同温度下可能出现的状态。interface WaterStateclass SubzeroState implements
10、WaterState结冰class NormalState implements WaterState冰冷或凉快class HotState implements WaterState有热气冒出,温热class BoiledState implements WaterState沸腾,烫class Water WaterState state; public void setState(WaterState s)public class CheckWaterState Water cupWater=new Water();水杯中的水在零下时: cupWater.setState(new SubzeroState(); cupWater.show();水杯中的水在常温时: cupWater.setState(new NormalState();水杯中的水在六十七度时: cupWater.setState(new HotState();水杯中的水在一XX时: cupWater.setState(new BoiledState();
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1