JAVA接口练习——编程例题之天气预报

  • Post author:
  • Post category:java


//weatherState.java
public interface weatherState{
public void showState();
}
//cloudyLittleState.java
public class cloudyLittleState implements weatherState{
public void showState(){
System.out.println("少云,晴");
}
//cloudyDayState.java
public class cloudyDayState implements weatherState{
public void showState(){
System.out.println("多云,阴");
}
}

//sunnyDayState.java
public class sunnyDayState implements weatherState{
public void showState(){
System.out.println("晴天");
}
}
//weather.java
public class weather{
weatherState wS;

public void setWeatherState(weatherState wS){
this.wS=wS;
}
public void show(){
wS.showState();
}
}
//weatherForecast.java
public class weatherForecast{
public static void main(String args[]){
weather guangzhouWeather=new weather();
System.out.print("白天:");
guangzhouWeather.setWeatherState(new sunnyDayState());
guangzhouWeather.show();
System.out.print("  转:");
guangzhouWeather.setWeatherState(new cloudyDayState());
guangzhouWeather.show();
System.out.print("夜间:");
guangzhouWeather.setWeatherState(new cloudyLittleState());
guangzhouWeather.show();
}
}

输出:



参考自 《java面向对象程序设计——实验指导与习题解答(张跃平等 著)》





版权声明:本文为qq_38329988原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。