JAVA 接口与实现 实验实例 歌手大赛 天气预报

接口相关知识点 

        接口体中有常量的声明(没有变量)和抽象方法声明而且接口体中所有的常量的访问权限一定都是public(允许省略public、final修饰符)所有的抽象方法的访问权限一定都是 public(允许省略public、abstract 修饰符)。

     类实现接口,以便绑定接口中的方法。一个类可以实现多个接口,类通过使用关键字 implements声明自己实现一个或多个接口。如果一个非抽象类实现了某个接口,那么这个类必须重写该接口的所有方法。

歌手大赛要求

        歌手大赛计算选手成绩的方法是去掉一个最高分和一个最低分后再计算平均分,而学校评估一个班级的学生的体重时,是计算全班同学的平均体重。SongGame类和School类都实现了ComputerAverage接口,但实现的方式不同。

CompurerAverage类 接口

public interface CompurerAverage {
	public double average(double x[]);
}

 Estimator类


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[] ={56,55.5,65,50,51.5,53.6,70,49,66,62,46};  
		CompurerAverage computer;
		computer=new SongGame();  
		double result = computer.average(a); //computer调用average(double x[])方法,将数组a传递给参数x
		System.out.printf("%n");
		System.out.printf("歌手最后得分:%5.3f\n",result);
		computer=new School();  
		result = computer.average(b);//computer调用average(double x[])方法,将数组b传递给参数x
		System.out.printf("学生平均体重:%-5.2f kg",result);
   }	
}

 School类


public class School implements CompurerAverage {
	public double average(double x[]){//重写public double average(double x[])方法,返回数组x[]的元素的算术平均
		double sum = 0;
		for (int i = 0; i < x.length; i++) {
			sum += x[i];
		}
		return sum/(double)x.length;
	}
}

 SongGame类


public class SongGame 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=i;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;
   }
}

School类如果不重写public double average(double x[ ])方法,程序编译时提示School类必须实现继承的抽象方法

结果

 

 天气预报要求

天气可能出现不同的状态,要求用接口封装天气的状态。具体要求如下。

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

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

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

(4)编写主类,在主类中进行天气预报。

 CloudyDayState类


public class CloudyDayState implements WeatherState {
	public void showState() {
		System.out.print("多云");
	}//重写public void showState()
}

CloudyLittleState类


public class CloudyLittleState implements WeatherState{
	public void showState() {
	    System.out.print("少云,有时晴.");
	}
}

 HeavyRainState类


public class HeavyRainState implements WeatherState{
    public void showState(){
    	System.out.print("暴雨");
    }//重写public void showState()
}

 LightRainState类


public class LightRainState implements WeatherState {
	public void showState() {
	    System.out.print("小雨");
	} //重写public void showState()方法
}

 Weather类


public class Weather {
	WeatherState  state;
	public void show() {
	    state.showState();
	}
	public void setState(WeatherState s) {
	    state = s;
	}
}

WeatherForecast类


public class WeatherForecast {
	public static void main(String args[]) {
	     Weather weatherBeijing =new Weather();
	     System.out.print("\n今天白天:");
	     weatherBeijing.setState(new CloudyDayState());
	     weatherBeijing.show();
	     System.out.print("\n今天夜间:");
	     weatherBeijing.setState(new LightRainState());
	     weatherBeijing.show();
	     System.out.print("转:");
	     weatherBeijing.setState(new HeavyRainState());
	     weatherBeijing.show();
	     System.out.print("\n明天白天:");
	     weatherBeijing.setState(new LightRainState());
	     weatherBeijing.show();
	     System.out.print("\n明天夜间:");
	     weatherBeijing.setState(new CloudyLittleState());
	     weatherBeijing.show();
	}
}

 WeatherState类 接口


public interface WeatherState {
	public void showState();
}

结果 

 

 为什么使用面向接口编程

        在设计程序时,经常会使用接口其原因是:接口只关心操作,但不关心这些操作具体实现的细节,可以使程序的设计者把主要精力放在程序的设计上,而不必拘泥于细节的实现(细节留给接口的实现者)即避免设计者把大量的时间和精力花费在具体的算法上。

        使用接口进行程序设计的核心技术之一是使用接口回调,即将实现接口的类的对象的引用放到接口变量中那么这个接口变量就可以调用类实现的接口方法。

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

  • 19
    点赞
  • 79
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kun.A.A

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值