黑马程序员 交通灯案例
---------------------- android培訓(xùn)、java培訓(xùn)、期待與您交流! ----------------------
? ?通過張老師的交通燈的例子我認(rèn)識(shí)到了很多:首先是要結(jié)合現(xiàn)實(shí)生活,明白交通燈的的邏輯,說實(shí)話在沒聽老師的課之前我真不知道交通燈的具體邏輯,其次認(rèn)識(shí)到了畫圖的重要性,單靠想,自己想一會(huì)就把自己繞進(jìn)去了,畫圖無疑是解決問題非常好的方法。 再次學(xué)到了面向?qū)ο蟮脑O(shè)計(jì)技巧。好了,下面來看這道題。
? 1、對象設(shè)計(jì):設(shè)計(jì)一個(gè)Road對象操作
設(shè)計(jì)一個(gè)Road類
思路:1.創(chuàng)建一個(gè)單線程,模擬車子出現(xiàn)的過程、
2.檢查是否為綠燈。
1 public class Road { 2 private List<String> vechicles = new ArrayList<String>(); 3 4 private String name =null; 5 public Road(String name){ 6 this.name = name; 7 8 //模擬車輛不斷隨機(jī)上路的過程 9 ExecutorService pool = Executors.newSingleThreadExecutor(); 10 pool.execute(new Runnable(){ 11 public void run(){ 12 for(int i=1;i<1000;i++){ 13 try { 14 Thread.sleep((new Random().nextInt(10) + 1) * 1000); 15 } catch (InterruptedException e) { 16 e.printStackTrace(); 17 } 18 vechicles.add(Road.this.name + "_" + i); 19 } 20 } 21 22 }); 23 25 ScheduledExecutorService timer = Executors.newScheduledThreadPool(1); 26 timer.scheduleAtFixedRate( 27 new Runnable(){ 28 public void run(){ 29 if(vechicles.size()>0){ 30 boolean lighted = Lamp.valueOf(Road.this.name).isLighted(); 31 if(lighted){ 32 System.out.println(vechicles.remove(0) + " is traversing !"); 33 } 34 } 35 36 } 37 }, 38 1, 39 1, 40 TimeUnit.SECONDS); 41 42 } 43 }
?
燈一共有十二種情況和路情況一樣 定義燈的枚舉?
枚舉的value方法。
package traffic;public enum Lamp {S2N("N2S","S2W",false),S2W("N2E","E2N",false),E2W("W2E","E2S",false),E2S("W2N","W2S",false),N2S(null,null,false),N2E(null,null,false),W2E(null,null,false),W2N(null,null,false),S2E(null,null,false),E2N(null,null,false),N2W(null,null,false),W2S(null,null,false);private boolean lighted;//對應(yīng)的燈private String opposite;//下一個(gè)燈private String next;private Lamp(String opposite,String next,boolean lighted){this.opposite =opposite;this.next=next;this.lighted=lighted;}private Lamp(){}public boolean isLighted(){return lighted;}public void light(){this.lighted=true;if(opposite!=null){Lamp.valueOf(opposite).lighted=true;System.out.println(name() + " lamp is green,下面總共應(yīng)該有6個(gè)方向能看到汽車穿過!");}}public Lamp blackOut(){this.lighted =false;if(opposite!=null){Lamp.valueOf(opposite).lighted=false;}Lamp nextLight =Lamp.valueOf(next);System.out.println("綠燈從" + name() + "-------->切換為" + next); if(next!=null)nextLight.lighted=true;return nextLight;}}
定義燈的控制類
package traffic;import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit;public class LampController {private Lamp currentLamp;public LampController(){currentLamp =Lamp.S2N;currentLamp.light();ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);timer.scheduleAtFixedRate(new Runnable(){public void run(){LampController.this.currentLamp =currentLamp.blackOut();}}, 10, 10, TimeUnit.SECONDS);}}
最后是控制的主類
package traffic;public class MainClass {public static void main(String[] args){String[] directions ={"S2N","S2W","E2W","E2S","N2S","N2E","W2E","W2N","S2E","E2N","N2W","W2S"};for(String direction:directions){new Road(direction);}new LampController();}}
總結(jié):枚舉的知識(shí)環(huán)節(jié)薄弱,有待加強(qiáng)。及時(shí)學(xué)習(xí)新知識(shí)。
---------------------- android培訓(xùn)、java培訓(xùn)、期待與您交流! ---------------------- 詳細(xì)請查看:http://edu.csdn.net/heima
轉(zhuǎn)載于:https://www.cnblogs.com/malinkang1989/archive/2012/06/21/2557841.html
總結(jié)
以上是生活随笔為你收集整理的黑马程序员 交通灯案例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 腾讯文档使用记录
- 下一篇: 洛谷 P5057 [CQOI2006]简