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面向对象程序设计——实验指导与习题解答(张跃平等 著)》

猜你喜欢

转载自blog.csdn.net/qq_38329988/article/details/80430176