您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“怎么利用Java實現天氣預報播報功能”,內容詳細,步驟清晰,細節處理妥當,希望這篇“怎么利用Java實現天氣預報播報功能”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
package lab2; public interface WeatherState { public void showState(); }
package lab2; public class Weather { WeatherState state; public void show() { state.showState(); } public void setState(WeatherState s) { state = s; } }
package lab2; 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(); } }
package lab2; public class CloudyLittleState implements WeatherState { public void showState() { System.out.print("少云,有時晴!"); } }
package lab2; public class CloudyDayState implements WeatherState{ @Override public void showState() { System.out.println("多云,為陰天!"); } }
package lab2; public class HeavyRainState implements WeatherState { @Override public void showState() { System.out.println("有大雨來臨!"); } }
package lab2; public class LightRainState implements WeatherState { @Override public void showState() { System.out.println("小雨,幾乎沒有雨!"); } }
讀到這里,這篇“怎么利用Java實現天氣預報播報功能”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。