黑马程序员--Java7K破解题---交通灯
2014-03-26 17:56
471 查看
切不可空想,一定要画图!
面向对象:红绿灯、红绿灯的控制系统、汽车、路线。
面向对象的重要经验:谁拥有数据,谁就对外提供操作这些数据的方法。
例子:人在黑板上画圆。(person、blackboard,circle三个对象,画圆的动作存在圆上面。
小球从绳子的一端滚到另一端的面向对象设计:
Lamp类表示一个交通灯。(12灯。)使用枚举创建。
线路的代码:
面向对象:红绿灯、红绿灯的控制系统、汽车、路线。
面向对象的重要经验:谁拥有数据,谁就对外提供操作这些数据的方法。
例子:人在黑板上画圆。(person、blackboard,circle三个对象,画圆的动作存在圆上面。
小球从绳子的一端滚到另一端的面向对象设计:
class Rope{ private Point start; private Point end; public Rope(Point start,Point end){ this.start=start; this.end=end; } public Point nexPoint(Point currentPoint){} } class Ball{ Private Rope rope; private Point currentPoint; public Ball(Rope rope,startPoint){ this.rope=rope; this.currentPoint=statPoint; } public void move(){ currentPoint=rope.nextPoint(currentPoint); System.out.prrintln("小球移动到了"+currentPoint); } }
Lamp类表示一个交通灯。(12灯。)使用枚举创建。
public enum Lamp { S2N("N2S",false,"S2W"),S2W("N2E",false,"E2W"), E2W("W2E",false,"E2S"),E2S("W2N",false,"S2N"), S2E("N2W",true,"S2E"),E2N("W2S",true,"E2n"), N2S(null,false,null),N2E(null,false,null),W2E(null,false,null), W2N(null,false,null),N2W(null,false,null),W2S(null,false,null); private Lamp(String opposite,boolean lighted,String next){ this.opposite=opposite; this.next=next; this.lighted=lighted; } private Lamp(){}; 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(); //opposite.light(); } System.out.println(name()+"lamp is green 下面有六路车通过"); } public Lamp blackOut(){ this.lighted=false; if(opposite !=null){ Lamp.valueOf(opposite).blackOut(); //opposite.light(); } Lamp nextLamp=null; if (next !=null){ nextLamp=Lamp.valueOf(next); System.out.println("l绿灯从"+name()+"----->切换为"+next); nextLamp.light(); } return nextLamp; } }
线路的代码:
import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class Road { private List<String> vechicles=new ArrayList<String>(); private String name=null; public Road(String name){ this.name=name; ExecutorService pool=Executors.newSingleThreadExecutor();//创建线程。 pool.execute(new Runnable(){ public void run(){ for(int i=1;i<1000;i++){ try { Thread.sleep((new Random().nextInt(10)+1)*1000);//随机产生车。 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } 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).isLighted(); if(lighted==true){ System.out.println( vechicles.remove(0)+" is travling"); } } } }, 1, 1, TimeUnit.SECONDS); } }交通灯控制器代码:
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(){ currentLamp=currentLamp.blackOut(); } }, 10, 10, TimeUnit.SECONDS); } }
相关文章推荐
- 黑马程序员—Java基础学习笔记之7K月薪面试题破解_交通灯管理系统
- 黑马程序员-张孝祥-7K月薪面试题破解之一_交通灯管理系统
- 黑马程序员java学习日记十六 7k面试题交通灯管理系统讲解
- 黑马程序员-----张孝祥-7K月薪面试题破解之一_交通灯管理系统视频教程观后总结
- 黑马程序员_7K月薪面试题破解之一_交通灯管理系统
- 【黑马程序员】Java7K面试题20:交通灯管理系统
- 黑马程序员 - Java 面试题破解 交通灯管理系统
- 黑马程序员--java7k破解题--银行调度业务
- 黑马程序员--java基础之交通灯管理系统(7K面试题)
- 黑马程序员_Java7K面试题——交通灯管理系统(第10篇)
- 黑马程序员_Java 7K面试题—交通灯管理系统
- 黑马程序员-7K面试题破解(交通灯管理系统)
- 黑马程序员_Java学习笔记之7K面试题交通等管理系统
- 【黑马程序员】java 7k面试题--交通灯管理系统
- 黑马程序员 Java自学总结二十一 张老师7K面试题之交通灯管理系统
- 黑马程序员 7K面试题破解:交通灯管理系统
- 黑马程序员_java 7k_交通灯管理系统
- 黑马程序员-----JAVA 7K月薪面试题:交通灯管理系统
- 黑马程序员-7K月薪面试题破解之一_交通灯管理系统
- <黑马程序员>月薪7K面试题破解-交通灯管理系统