面向对象编程——红绿灯车辆能否通过问题
Posted 一只醉虾
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了面向对象编程——红绿灯车辆能否通过问题相关的知识,希望对你有一定的参考价值。
红灯亮时,禁止车辆通行,准许右转弯。
黄灯亮时,禁止车辆通行,如已过停车线需继续前进。
绿灯亮时,准许车辆通行。
小车可能会经过普通红绿灯路口,也可能经过导向红绿灯路口
请使用面向对象方法编写车辆能不能通行的逻辑实现
这题就是红绿灯设计题的简化版,笔试时间紧没有设计vehicle对象,将就着看
Lamp类
public enum Lamp {
//R红 Y黄 G绿
red("red","green",0),green("green","yellow",0),yellow("yellow","red",0),
right("right",null,1);//添加一个判断右转的灯
private String name=null;
private String nextName=null;
private int light;
private Lamp(String name,String nextName,int light){
this.name=name;
this.nextName=nextName;
this.light=light;
}
//获取当前交通灯的状态
public int isLight(){
return light;
}
//变更右转灯
public void trunRightLight(){
this.light=Math.random()>0.5?0:1;
System.out.println("right changes to "+this.light);
}
//亮灯
public void trunLighted(){
this.light=1;//点灯
System.out.println(this.name+" is lighted");
}
public Lamp blackOut(){
this.light=0;//灭灯
Lamp nextLamp=null;
nextLamp=Lamp.valueOf(Lamp.this.nextName);
nextLamp.trunLighted();//点亮下一个灯
System.out.println(this.name+" truns "+nextLamp.name);
return nextLamp;
}
}
Road类
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Road {
private String roadName;
public Road(String roadName){
this.roadName=roadName;
ScheduledExecutorService timer= Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(
new Runnable() {
@Override
public void run() {
int right=Lamp.valueOf("right").isLight();//是否导向路口
int green=Lamp.valueOf("green").isLight();//绿灯亮了没
int yellow=Lamp.valueOf("yellow").isLight();//
int red=Lamp.valueOf("red").isLight();//
int line=Math.random()>0.5?0:1;//随机模拟,0就没过线,1就过线了
//红灯
if(red==1){
if(right==1){
System.out.println("the vehichle only can trun right.");
}else {
System.out.println("the vehichle can\'t pass.");
}
}
//绿灯
if(green==1){
if(right==1){
System.out.println("the vehichle can go straight or turn right.");
}else {
System.out.println("the vehichle can only go straight.");
}
}
//黄灯过线可以直行,不过线不给走
if(yellow==1){
if(line==1){
System.out.println("the vehichle can go straight");
}else {
System.out.println("the vehichle only can trun right.");
}
}
}
},
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 lamp;
private Lamp lamp2;
private LampController(){
lamp=Lamp.yellow;//指定初始为绿灯
lamp.trunLighted();//点亮绿灯
lamp2=Lamp.right;
lamp2.trunRightLight();//随机点亮
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
lamp=lamp.blackOut();
lamp2.trunRightLight();//随机点亮
}
},5,5, TimeUnit.SECONDS);
}
public static final LampController lampController=new LampController();
}
Test类
public class test {
public static void main(String[] args) {
LampController lampController=LampController.lampController;
new Road("Stright");
}
}
以上是关于面向对象编程——红绿灯车辆能否通过问题的主要内容,如果未能解决你的问题,请参考以下文章