黑马程序员 交通灯管理系统

---------------------- ASP.Net+Android+IOS开发、.Net培训 期待与您交流!----------------------

 

关于交通灯管理系统的个人学习心得:

1. 分析面向对象的技巧:谁拥有数据,谁就对外提供操作这些数据的方法

面向对象的案例分析:

(1)人在黑板上画圆
对象:
person,blackboard,circle
画圆这个方法应该属于圆这个对象
因为画圆这个方法中必定会有圆心(x,y),半径(radius)
这些数据都来自于圆的属性。

(2)将2块石头磨成石刀,用石刀砍树得到木材,用木材做成椅子
对象:
stone
stoneKnife=KnifeFactory.createKnife(stone1,stone2)
tree
material=stoneKnife.cut(tree)
chair=ChairFactory.makeChair(material)

2. 局部内部类访问外部类的成员变量时的格式:

外部类名.this.成员变量名

3. ArrayList类中的remove方法的作用是移除指定位置上的元素并返回移除的这个元素

 Eremove(int index)

4. Executes是一个执行线程的工具类,真正的线程池接口是ExecutorService

5. ScheduledExecutorService 此接口的功能和Time/TimerTask类似,主要解决需要重复执行的任务

6. Executors.newSingleThreadExecutor().execute(command);

创建一个单线程的线程池。这个线程池只有一个线程在工作,也就是相当于单线程串行执行所有任务。如果这个唯一的线程因为异常结束,那么会有一个新的线程来替代它。此线程池保证所有任务的执行顺序按照任务的提交顺序执行。

7. Executors.newScheduledThreadPool(corePoolSize).scheduleAtFixedRate(command, initialDelay, period, unit);

创建一个大小无限的线程池。此线程池支持定时以及周期性执行任务的需求。

/**
 * 路
 * 此类的功能:通过增加车辆和减少车辆来模拟车移动的过程,故
 * 车只需定义成一个字符串即可
 * 一共有12个方向的车,在这12个方向中其中有4个方向是主要的
 * 这4个方向分别是:
 * S2N		S2W		E2W		E2S
 * 这四个方向的相反方向是:
 * N2S		N2E		W2E		W2N
 * 4个右拐弯方向是:
 * S2E		E2N		N2W		W2S
 * 只有前4个主要方向上的灯有相反方向上的灯
 * 也有下一个方向上的灯,这样做的目的是好控制
 * 灯的亮与灭
 * 假设4个右拐弯的灯是常绿的,这样做的目的是保证
 * 每个方向上都有灯,这样路只关注自己所在前方上的灯
 * 若灯是亮的则移除一辆车,路还要不停的产生车,还要
 * 定时的检测自己所在方向的灯是否是亮的
 */
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class Road {
	//创建存储车辆的集合
	private List<String> vehicle=new ArrayList<String>();
	
	//每一个方向的名称
	private String name;
	public Road(String name){
		this.name=name;
		
		//车不停的上路和车穿过马路这些功能是互不影响的,所以要用多线程来完成各自的功能
		Executors.newSingleThreadExecutor().execute(new Runnable(){
			public void run(){
				for(int x=1; x<1000; x++){
					try {
						Thread.sleep((new Random().nextInt(10)+1)*1000); //1-10s内产生一辆车
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					vehicle.add(Road.this.name+"_"+x); //内部类访问外部类成员变量:外部类.this.成员变量名
				}
			}
		});
		
		//定时的检测相应方向上的灯是否是亮的
		Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
				new Runnable(){
					public void run(){
						if(vehicle.size()>0){//若路上有车则判断该方向上的灯是否是亮的,若是亮的则移除一辆车
							boolean lighted=Lamp.valueOf(Road.this.name).isLigth();
							if(lighted){
								System.out.println(vehicle.remove(0)+" is traving."); //返回移除的车
							}
						}
					}
				}, 
				1, 
				1,
				TimeUnit.SECONDS
		);
		
	}
	
}

 

/**
 * 灯
 * 灯的个数是固定的所以定义成枚举
 * 一共有12个方向故应该有12个方向的灯
 * 灯有变亮和不亮的方法
 */
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 String opposite;
	 private String next;
	 private boolean lighted;
	 private Lamp(String opposite,String next,boolean lighted){
		 this.opposite=opposite;
		 this.next=next;
		 this.lighted=lighted;
	 }
	 
	 //判断灯是否是亮的
	 public boolean isLigth(){
		 return this.lighted;
	 }
	 
	 //让当前的灯变亮,若当前的灯有相反方向的灯,让当前灯的相反方向的灯变亮
	 public void light(){
		 this.lighted=true;
		 if(this.opposite!=null){
			 Lamp.valueOf(opposite).light();
		 }
		 System.out.println(name()+" is green.下面总共有6个方向可以看到汽车穿过");
	 }
	 
	 //让当前的灯变灭,若当前的灯有相反方向的灯,让当前灯的相反方向的灯变灭;
	 //若当前灯有下一个方向上的灯,让下一个方向上的灯变亮
	 public Lamp blackOut(){
		 this.lighted=false;
		 if(this.opposite!=null){
			 Lamp.valueOf(opposite).blackOut();
		 }
		 
		 Lamp nextLamp=null;
		 if(this.next!=null){
			 nextLamp=Lamp.valueOf(next);
			 System.out.println("绿灯由"+name()+"切换为------>"+next);
			 nextLamp.light();
		 }
		 return nextLamp;
	 }
	 
}

 

import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * 交通灯控制系统
 * 定时的将灯由绿(亮)变红(灭),再由红变绿
 */
public class LampController {
	private Lamp currentLamp;
	public LampController(){
		this.currentLamp=Lamp.S2N;
		currentLamp.light();
		
		Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
				new Runnable(){
					public void run(){
						currentLamp=currentLamp.blackOut();
					}
				},
				10,
				10,
				TimeUnit.SECONDS
		);
		
	}
}

 

/**
 * 测试类
 * @author Administrator
 *
 */
public class MainClass {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//创建12个方向
		String[] directors={"S2N","S2W","E2W","E2S","N2S","N2E","W2E","W2N","S2E","E2N","N2W","W2S"};
		
		//创建12条路
		for(int x=0; x<directors.length; x++){
			new Road(directors[x]);
		}
		
		//创建控制系统
		new LampController();
	}

}


---------------------- ASP.Net+Android+IOS开发、.Net培训 期待与您交流!----------------------

详细请查看:http://edu.csdn.net

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值