传智播客-traffic Lamp-day10

---------------------- android培训java培训、期待与您交流! ----------------------

7k------traffic Lamp
面向对象的思想:谁拥有数据,谁就提供方法。

(4~6课为思路,7为road,8为定时器,9为灯的亮暗,10灯的控制器)
用到的方法:匿名内部类,枚举enum,集合arrayList,
     多线程Thread.sleep(),1.5的线程池Executors.newSingleThreadExecutor()
需要记忆的单词:
Executors -->线程池
Schedule-->计划表
directions-->方向
South-->北
North-->南
East--->东
West--->西
由于一个方向上有三条路线,及直行,左转,右转。
于是就有了12条线路:
 1,南出发:S2N(直行),S2W(左转),S2E(右转)
 3,北出发:N2S(直行),N2E(左转),N2W(右转)

 2,东出发:E2W(直行),E2S(左转),E2N(右转)
 4,西出发:W2E(直行),W2N(左转),W2S(右转)

因为南和北、东和西是对应的,也就是相反的,所以:
 1,N2S(直行)是S2N(直行)反方向
 2,S2W(左转)是N2E(左转)反方向
 3,E2W(直行)是W2E(直行)反方向
 4,E2S(左转)是W2N(左转)反方向
这样考虑的线路就是8条线路:
N2S,S2W,E2W,E2S加4个右转S2E,N2W,E2N,W2S
车通行是看灯的,绿灯通行,红灯停。为统一信号灯,右转设置为绿灯常亮。
为简化程序,灯不考虑时间,而且一次只能过一辆车。
这样就只用到三个对象了:路,灯,控制系统

Road:  
private List<String> vechicles=new arrayList<String>();//这里用List而不用

arrayList是为了面象接口编程(原来的多态)
private String name=null;
public Road(String name)
 {
 this.name=name;
 ExecutorService pool=Executors.newSingleThreadExecutor();//Executors是

1.5线程池里的方法,记住就可以了。
 pool.execute(new Runnable(){ 
//线程匿名内部类
  public void run()
   {
   for(int i=1;i<1000;i++)
   {
    try
    {
     Thread.sleep((new Random().newxtInt(10)

+1)*1000);
    }
    catch(InterruptedException e)
    {
     e.printStrackTrace();
    }
    vechicles.add(Road.this.name+"-"+i);
   }
  }
 });
 ScheduledExecutorService timer=Executors.newScheduledThreadPool(1);// 定

时器
 timer.scheduleAtFixedRate(
  new Runnable(){
   public void run(){
    if(vechicles.size()>0){
     boolean lighted=Lamp.valueOf

(Road.this.name).isLight();
     if(lighted){
      Sop(vechicles.remove(0)+" is

traversing!");
     }
    }
   }
  ),
  1,
  1,
  TimeUnit.SECONDS);
//定时器的第一个参数是要运行的代码,第二个是多久运行,第三个是再次运行的时间,第

四个是时间的单位。

内部类访问外部类的成员变量,外部类要加final修饰。这里张老师用了类名.this.name

Lamp: lighted  //亮或不亮及true or false
 opposite  //对面的灯
 next  //下一个灯
public enum Lamp{
 S2N("N2S","S2W",false),S2W("N2E","E2W",false),E2W

("W2E","E2S",false),E2S("W2N","S2N",false),
//第一个参数对面灯,第二个是下一个灯,第三个是这个灯是否亮
 N2S(null,null,false),N2E(null,null,false),W2E(null,null,false),W2N

(null,null,false),
 S2E(null,null,true),E2N(null,null,true),N2W(null,null,true),W2S

(null,null,true);

 private Lamp(String opposite,String next,boolean lighted){
  this.opposite=opposite;
  this.next=next;
  this.lighted=lighted;
 }
 private boolean lighted;
 private String opposite;
 private String next;
 public boolean isLighted(){
  return lighted;
 }

//当前灯变绿,对应灯也变绿
 public void light(){
  this.lighted=true;
  if(opposite!=null){
   Lamp.valueOf(opposite).light();
  }
 }
//当前灯变红,对应灯也变红
 public Lamp blackOut(){
  this.lighted=false;
  if(opposite!=null){
   Lamp.valueOf(opposite).blackOut();
  }
//下一个灯要变绿
  Lamp nextLamp=null;
/*开始这里为nextLamp=Lamp.valueOf(next);但当nextLamp为null时,.valueOf(next)会出

问题,所以将下一个灯的方法移动到if语句里面。*/
  if(next!=null){
   nextLamp=Lamp.valueOf(next);
   Lamp.valueOf(opposite).light();
  }
  return nextLamp;
}


Control:currentLamp //当前灯
 定时器改变当前灯,返回下一个灯

public class LampController{
 private Lamp currentLamp;
 public LampController(){
  currentLamp=Lamp.S2N;
  currentLamp.light();
  ScheduledExecutorService timer=Executors.newScheduledThreadPool

(1);
  timer.scheduleFixedRate(
   new Runnable(){
    public void run(){
     currentLamp=currentLamp.blackOut();
    }
   },
   10,
   10,
   TimeUnit.SECONDS);
 }
}

main:
String [] directions=new String[]

{"S2N","S2W","E2W","E2S","N2S","N2E","W2E","W2N","S2E","E2N","N2W","W2S"};
for(int i=0,i<directions.length;i++){
 new Road(directions[i]);
//???这里new directions为什么会是Road()???
//答:现在是主函数,现在要new的是road的对象,也就是路的。
}
new LampController();

---------------------- android培训java培训、期待与您交流! ----------------------
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值