1.评价成绩
体操比赛计算选手成绩的办法是去掉一个最高分和最低分后再计算平均分,而学校考察一个班级的某科目的考试情况时,是计算全班同学的平均成绩。Gymnastics类和School类都实现了ComputerAverage接口,但实现的方式不同。
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;i<count;i++){
for(int j=1;j<count;j++){
if(x[j]<x[i]) {
temp=x[j];
x[j]=x[i];
x[i]=temp;
}
}
}
for(int i=1;i<count-1;i++){
aver=aver*x[i];
}
if(count>2)
aver=aver/(count-2);
else
aver=0;
return aver;
}
}
class School implements CompurerAverage {
public double average(double[] x) {
int count=x.length;
double aver=0;
for(int i=0;i<count;i++){
aver=aver+x[i];
}
aver=aver/count;
return aver;
}
}
public class Estimator{
public static void main(String arg[]){
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=computer.average(a);
System.out.printf("%n");
System.out.printf("体操选手最后得分:%5.3f\n",result);
computer=new School();
result=computer.average(b);
System.out.printf("班级考试平均分数:%-5.2f",result);
}
}
2.货车的装载量
货车要装载一批货物,货物由三种商品组成:电视、计算机和洗衣机。卡车需要计算这整批货物的重量。
要求有一个ComputeWeight接口,该接口有一个方法:
public double computeweight()
有三个实现该接口的类:Television、Computer和WashMachine.这三个类通过实现接口computeTotaSales给出自重
有一个Truck类,该类用ComputeWeight接口类型的数组作为成员(Truck类面向接口)那么该数组的单元就可以存放Television对象的引用、Computer对象的引用或WashMachine对象的引用。程序能输出Truck对象所装载的货物的总重量
interface ComputerWeight{
public double computeWeight();
}
class Television implements ComputerWeight {
//[代码1]重写computeWeight()方法
public double computeWeight(){
return 3.5;
}
}
class Computer implements ComputerWeight {
//[代码2]重写computeWeight()方法
public double computeWeight(){
return 2.67;
}
}
class implements ComputerWeight {
//[代码3]重写computeWeight()方法
public double computeWeight(){
return 13.8;
}
}
class Truck{
ComputerWeight[] goods;
double totalWeights=0;
Truck(ComputerWeight[] goods){
this.goods=goods;
}
public void setGoods(ComputerWeight[] goos){
this.goods=goods;
}
public double getTotalWeights(){
totalWeights=0;
//[代码4]计算totalWeights
for(int i=0;i<goods.length;i++){
totalWeights += goods[i].computeWeight();
}
return totalWeights;
}
}
public class CheckCarWeight{
public static void main(String args[]){
ComputerWeight[] goods=new ComputerWeight[650]; //650件货物
for(int i=0;i<goods.length;i++){
if(i%3==0)
goods[i]=new Television();
else if(i%3==1)
goods[i]=new Computer();
else if(i%3==2)
goods[i]=new WashMachine();
}
Truck truck=new Truck(goods);
System.out.printf("\n货车装载的货物重量:%-8.5f kg\n",truck.getTotalWeights());
goods=new ComputerWeight[68]; //68件货物
for(int i=0;i<goods.length;i++){
if(i%2==0)
goods[i]=new Television();
else
good[i]=new WashMachine();
}
truck.setGoods(goods);
System.out.printf("货车装载的货物重量:%-8.5f kg\n",truck.getTotalWeights());
}
}
3.小狗的状态
小狗在不同环境条件下可能呈现不同的状态表现,要求用接口封装小狗的状态。具体要求如下。
(1) 编写一个接口DogState,该接口有一个名字为void showState()的方法
(2) 编写Dog类,该类有一个名字为void showState()的方法。另外,该类有一个show()方法,在该方法中让接口state回调showState()的方法
(3) 编写若干个实现DogState接口的类,负责刻画小狗的各种状态
(4) 编写主类,在主类中测试小狗的各种状态
interface DogState{
public void showState();
}
class SoftlyState implements DogState {
public void showState(){
System.out.println("听主人的命令!");
}
}
class MeetEnemyState implements DogState {
//[代码1]重写public void showState()方法
public void showState(){
System.out.println("上去咬一口!");
}
}
class MeetFriendState implements DogState {
//[代码2]重写public void showState()方法
public void showState(){
System.out.println("晃动尾巴,以示友好!");
}
}
class MeetAnotherDog implements DogState {
//[代码3]重写public void showState()方法
public void showState(){
System.out.println("hello!");
}
}
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.println("狗在主人面前:");
yellowDog.setState(new SoftlyState());
yellowDog.show();
System.out.println("狗遇到敌人:");
yellowDog.setState(new MeetEnemyState());
yellowDog.show();
System.out.println("狗遇到朋友:");
yellowDog.setState(new MeetFriendState());
yellowDog.show();
System.out.println("狗遇到同伴:");
yellowDog.setState(new MeetAnotherDog());
yellowDog.show();
}
}
接口体内只有常量的声明(没有变量)和抽象方法声明。而且接口体中所有的常量的访问权限一定都是public(允许省略public、final装饰符),所有的抽象方法的访问权限一定都是public(允许省略public、abstract修饰符)。接口由类去实现以便绑定接口的方法,一个类可以实现多个接口,类通过使用关键字implements声明自己实现一个或多个接口。如果一个非抽象类实现了某个接口,那么这个类必须重写该接口的所有方法。
接口回调是多态的另一种体现,接口回调是指:可以把使用某一接口的类创建的对象的引用赋给该接口声明的接口变量中,那么该接口变量就可以调用被类实现的接口中的方法,这一过程被称为对象功能接口的回调。不同类在使用同一接口时,可能具有不同的功能体现,即接口的方法体不必相同。因此,接口回调可能产生不同的行为。
在设计程序时,经常会使用接口,其原因是,接口只关心操作,但不关心这些操作具体实现的细节,可以使程序的设计者把主要精力放在程序的设计上,而不必拘泥于细节的实现(细节留给细节的实现者),即避免设计者把大量的时间和精力花费在具体的算法上。使用接口进行程序设计的核心技术之一是使用接口回调,即将实现接口的类的对象的引用放到接口变量中,那么这个核心变量就可以调用类实现的接口方法。所谓面向接口编程,是指当设计某种重要的类时,不让该类面向具体的类,而是面向接口,即所设计类中的重要数据是接口声明的变量,而不是具体类的对象。