---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ---------------------
思考:
在我们拿到这样一个题目时,首先要用面向对象的方式来思考它,
1、可以分为那几个类
2、各个类之间有什么联系
3、各个类的职责是什么
首先我们可以从这个题目中抽取出四个对象,灯、路、汽车、灯的控制器
路:应该有一个集合可以存储这条路上的汽车,并且提供对汽车的增删方法,并且启动一个线程为路增添汽车
灯:用一个枚举来构造12个方向的灯,并且每个灯都应该有以下属性:下一个灯,相反方向的灯,因为,这个灯变绿则相反方向的灯也应该变绿,而这个灯变红则下一个灯就要变绿
灯的控制器:隔一段时间用来控制等的变化
Lamp类
public enum Lamp {
N2S(null, null, false), N2E(null, null, false), W2E(null, null, false), W2N(
null, null, false), S2N("N2S", "S2W", false), S2W("N2E", "E2W",
false), E2W("W2E", "E2S", false), E2S("W2N", "S2N", false), // 我们要知道始终只有这四个灯在不停的切换
S2E(true), E2N(true), N2W(true), W2S(true);
private Lamp(String oppositeLamp, String nextLamp, boolean state) {
this.oppositeLamp = oppositeLamp;
this.nextLamp = nextLamp;
this.state = state;
}
private Lamp(boolean state) {
this.state = state;
}
private String oppositeLamp;
private String nextLamp;
private boolean state;
public void light() {
this.state = true;
if (oppositeLamp != null) {
Lamp lamp = Lamp.valueOf(oppositeLamp);
lamp.light();
}
}
public Lamp lightBlack() {
this.state = false;
if (oppositeLamp != null) {
Lamp.valueOf(oppositeLamp).lightBlack();
}
Lamp lamp = null;
if (nextLamp != null) {
lamp = Lamp.valueOf(nextLamp);
lamp.light();
}
return lamp;
}
public boolean isLight() {
return state;
}
}
Road 类
import java.util.ArrayList;
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 String name;
private ArrayList<String> list = new ArrayList<String>();
public Road(final String name) {
this.name = name;
ExecutorService es = Executors.newFixedThreadPool(1);
es.execute(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
// TODO Auto-generated method stub
list.add(name + i);
System.out.println(name + "路上来车了,这是这条路上第" + i + "辆车");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
ScheduledExecutorService ses = Executors.newScheduledThreadPool(1);
ses.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
if (list.size() > 0) {
if (Lamp.valueOf(Road.this.name).isLight()) {
list.remove(0);
System.out.println(Road.this.name + "路上走了一辆车,现在还有"
+ list.size());
}
}
}
}, 1, 1, TimeUnit.SECONDS);
}
}
LampController
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class LampController {
private Lamp currentLamp=Lamp.S2N;
public LampController()
{
currentLamp.light();
ScheduledExecutorService ses=Executors.newScheduledThreadPool(1);
ses.scheduleAtFixedRate(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
Lamp next=currentLamp.lightBlack();
System.out.println("灯要变化了,"+next+"方向的....................................灯变绿了");
//next.light();
}
}, 2, 2, TimeUnit.SECONDS);
}
}
Test类
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
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 LampController();
}
}
---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ---------------------