---------------------- android培训、java培训、期待与您交流! ----------------------
通过张老师的交通灯的例子我认识到了很多:首先是要结合现实生活,明白交通灯的的逻辑,说实话在没听老师的课之前我真不知道交通灯的具体逻辑,其次认识到了画图的重要性,单靠想,自己想一会就把自己绕进去了,画图无疑是解决问题非常好的方法。 再次学到了面向对象的设计技巧。好了,下面来看这道题。
1、对象设计:设计一个Road对象操作
设计一个Road类
思路:1.创建一个单线程,模拟车子出现的过程、
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 //模拟车辆不断随机上路的过程 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; //对应的灯 private String opposite; //下一个灯 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,下面总共应该有6个方向能看到汽车穿过!"); } } 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(); } }
总结:枚举的知识环节薄弱,有待加强。及时学习新知识。
---------------------- android培训、java培训、期待与您交流! ---------------------- 详细请查看:http://edu.csdn.net/heima