java接口练习4(多态)

//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
版权声明:本文为博主原创文章,转载请附上博文链接!

posted @ 2018-11-05 10:37  代码缔造的帝国  阅读(572)  评论(0编辑  收藏  举报