gzhnan

//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();
}
}


输出:


---------------------
作者:GHLANCE
来源:CSDN
原文:https://blog.csdn.net/qq_38329988/article/details/80430176
版权声明:本文为博主原创文章,转载请附上博文链接!

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-07-22
  • 2021-12-05
  • 2021-12-05
  • 2021-11-16
  • 2021-12-05
  • 2021-12-05
  • 2021-12-05
猜你喜欢
  • 2021-12-05
  • 2021-10-30
  • 2021-12-05
  • 2021-05-27
  • 2021-09-18
  • 2021-07-22
  • 2021-12-05
相关资源
相似解决方案