using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;namespace L6_4
{
class Program
{
static void Main(string[] args)
{
Cross cross1 = new Cross();
Car car1 = new Car((Postion)0,( Postion)2, cross1);
Car car2 = new Car((Postion)1, (Postion)3, cross1);
Console.WriteLine("车的ID号是:"+car1.CarID +"车要前往的方向:"+ car1.ToPos+"汽车是否通过:"+car1.isPassed);
Console.WriteLine("车的ID号是:"+car2.CarID + "车要前往的方向:"+ car2.ToPos);
}
public class Cross
{
private bool light1;
private bool light2=false ;
private bool light3;
private bool light4=false; public bool Light1 { get; set; }
public bool Light2 { get; set; }
public bool Light3 { get; set; }
public bool Light4 { get; set; }
public event EventHandler OnShiftLight;
public void time()
{
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(TimeEvent);
// 设置引发时间的时间间隔 此处设置为1秒(1000毫秒)
aTimer.Interval = 30000;
aTimer.Enabled = true;
}
private void TimeEvent(object source, ElapsedEventArgs e)
{
if (light1 && light3)
{
light1 = false;
light3 = false;
}
else if (light2 && light4)
{
light2 = false;
light4 = false;
}
else if (!light1 && !light3)
{
light2 = true;
light4 = true;
}
else if (!light2 && !light4)
{
light2 = true;
light4 = true;
}
}
}
public class Car
{
private static int count = 0; // CarID
public int CarID { get; private set; }
public Postion Pos { get; set; } // 当前的位置
public Postion ToPos { get; set; } // 要驶向的方位
public State CurState { get; set; } // 当前的状态
private Cross cross;
public bool isPassed { get; private set; } public Car(Postion from, Postion to, Cross cross)
{
count++;
CarID = count; CurState = State.Stop; //初始为停止
isPassed = false; Pos = from;
ToPos = to;
this.cross = cross; if (cross != null)
cross.OnShiftLight += new EventHandler(Run);
} public void Run(object sender, EventArgs e) // 根据十字路口路灯状态指示决定是否启动
{
if ((cross.Light1 && ToPos == Postion.East)
|| (cross.Light2 && ToPos == Postion.South)
|| (cross.Light3 && ToPos == Postion.West)
|| (cross.Light4 && ToPos == Postion.North))
{
CurState = State.Running;
Pos = ToPos;
isPassed = true;
}
}
} public enum State
{
Running,
Stop,
} public enum Postion
{
East,
South,
West,
North
}
}
}谁能帮我看看怎么才能触发事件OnShiftLight?这个是模拟红绿灯十字路口
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;namespace L6_4
{
class Program
{
static void Main(string[] args)
{
Cross cross1 = new Cross();
Car car1 = new Car((Postion)0,( Postion)2, cross1);
Car car2 = new Car((Postion)1, (Postion)3, cross1);
Console.WriteLine("车的ID号是:"+car1.CarID +"车要前往的方向:"+ car1.ToPos+"汽车是否通过:"+car1.isPassed);
Console.WriteLine("车的ID号是:"+car2.CarID + "车要前往的方向:"+ car2.ToPos);
}
public class Cross
{
private bool light1;
private bool light2=false ;
private bool light3;
private bool light4=false; public bool Light1 { get; set; }
public bool Light2 { get; set; }
public bool Light3 { get; set; }
public bool Light4 { get; set; }
public event EventHandler OnShiftLight;
public void time()
{
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(TimeEvent);
// 设置引发时间的时间间隔 此处设置为1秒(1000毫秒)
aTimer.Interval = 30000;
aTimer.Enabled = true;
}
private void TimeEvent(object source, ElapsedEventArgs e)
{
if (light1 && light3)
{
light1 = false;
light3 = false;
}
else if (light2 && light4)
{
light2 = false;
light4 = false;
}
else if (!light1 && !light3)
{
light2 = true;
light4 = true;
}
else if (!light2 && !light4)
{
light2 = true;
light4 = true;
}
}
}
public class Car
{
private static int count = 0; // CarID
public int CarID { get; private set; }
public Postion Pos { get; set; } // 当前的位置
public Postion ToPos { get; set; } // 要驶向的方位
public State CurState { get; set; } // 当前的状态
private Cross cross;
public bool isPassed { get; private set; } public Car(Postion from, Postion to, Cross cross)
{
count++;
CarID = count; CurState = State.Stop; //初始为停止
isPassed = false; Pos = from;
ToPos = to;
this.cross = cross; if (cross != null)
cross.OnShiftLight += new EventHandler(Run);
} public void Run(object sender, EventArgs e) // 根据十字路口路灯状态指示决定是否启动
{
if ((cross.Light1 && ToPos == Postion.East)
|| (cross.Light2 && ToPos == Postion.South)
|| (cross.Light3 && ToPos == Postion.West)
|| (cross.Light4 && ToPos == Postion.North))
{
CurState = State.Running;
Pos = ToPos;
isPassed = true;
}
}
} public enum State
{
Running,
Stop,
} public enum Postion
{
East,
South,
West,
North
}
}
}谁能帮我看看怎么才能触发事件OnShiftLight?这个是模拟红绿灯十字路口
public bool Light1 { get; set; }
public bool Light2 { get; set; }
public bool Light3 { get; set; }
public bool Light4 { get; set; }在Cross的灯set的时候
if(this.OnShiftLight!=null)
{
this.OnShiftLight(....);
}
class Program2
{
static void Main(string[] args)
{ Cross cross1 = new Cross();
cross1.OnShiftLight += new EventHandler(cross1_OnShiftLight);
Car car1 = new Car((Postion)0, (Postion)2, cross1);
Car car2 = new Car((Postion)1, (Postion)3, cross1); cross1.CarList.Add(car1);
cross1.CarList.Add(car2);
while (true)
{
Console.WriteLine("车的ID号是:" + car1.CarID + "车要前往的方向:" + car1.ToPos + "汽车是否通过:" + car1.isPassed);
Console.WriteLine("车的ID号是:" + car2.CarID + "车要前往的方向:" + car2.ToPos + "汽车是否通过:" + car2.isPassed);
System.Threading.Thread.Sleep(3000);
}
} static void cross1_OnShiftLight(object sender, EventArgs e)
{
foreach (Car item in ((Cross)sender).CarList)
{
item.Run();
}
} public class Cross
{
public bool Light1 { get; set; }
public bool Light2 { get; set; }
public bool Light3 { get; set; }
public bool Light4 { get; set; }
public List<Car> CarList { get; set; }
public event EventHandler OnShiftLight; public Cross()
{
CarList = new List<Car>();
System.Timers.Timer aTimer = new System.Timers.Timer();
// 设置引发时间的时间间隔 此处设置为1秒(1000毫秒)
aTimer.Elapsed += new ElapsedEventHandler(TimeEvent);
aTimer.Interval = 3000;
aTimer.Enabled = true;
aTimer.Start();
} private void TimeEvent(object source, ElapsedEventArgs e)
{
Light1 = !Light1;
Light2 = !Light2;
Light3 = !Light3;
Light4 = !Light4;
//if (light1 && light3)
//{
// light1 = false;
// light3 = false;
//}
//else if (light2 && light4)
//{
// light2 = false;
// light4 = false;
//}
//else if (!light1 && !light3)
//{
// light2 = true;
// light4 = true;
//}
//else if (!light2 && !light4)
//{
// light2 = true;
// light4 = true;
//}
if (OnShiftLight != null)
{
EventArgs ea = new EventArgs();
OnShiftLight(this, ea);
}
}
}
public class Car
{
private static int count = 0; // CarID
public int CarID { get; private set; }
public Postion Pos { get; set; } // 当前的位置
public Postion ToPos { get; set; } // 要驶向的方位
public State CurState { get; set; } // 当前的状态
private Cross cross;
public bool isPassed { get; private set; } public Car(Postion from, Postion to, Cross cross)
{
count++;
CarID = count; CurState = State.Stop; //初始为停止
isPassed = false; Pos = from;
ToPos = to;
this.cross = cross;
} public void Run() // 根据十字路口路灯状态指示决定是否启动
{
if ((cross.Light1 && ToPos == Postion.East)
|| (cross.Light2 && ToPos == Postion.South)
|| (cross.Light3 && ToPos == Postion.West)
|| (cross.Light4 && ToPos == Postion.North))
{
CurState = State.Running;
Pos = ToPos;
isPassed = true;
}
else
{
CurState = State.Stop;
Pos = ToPos;
isPassed = false;
}
}
} public enum State
{
Running,
Stop,
} public enum Postion
{
East,
South,
West,
North
}
}
大概给你改了一下,代码逻辑自己再理清一点。