Oop .NET飞机模拟器
用例名称:开始飞机模拟 经营范围:飞机飞行模拟器 级别:用户目标 主要参与者:用户Oop .NET飞机模拟器,oop,c#-2.0,Oop,C# 2.0,用例名称:开始飞机模拟 经营范围:飞机飞行模拟器 级别:用户目标 主要参与者:用户 用户启动飞机模拟器 向用户询问最大高度(天花板) 要求用户提供最低高度(地板) 飞机模拟器从空中位置开始,不起飞或着陆 飞机上升到最高高度 飞机下降到最低高度 重复步骤5和6,直到用户结束模拟 这是我的问题。在.NET中,哪个计时器最适合飞机类,应该是Windows窗体计时器、基于服务器的计时器还是线程计时器?我试图让飞机以计时器的间隔决定的速度上升/下降。希望这是有道理的 我需要一些澄清,请帮助!这是我的班级
级飞机
{
公共飞机()
{
_当前高度=0;
定时器_Timer=新定时器();
_timer.Start();
控制台写入线(“飞机启动”);
Console.ReadKey();
}
public const int MAXALLOWABLEHEIGHT=30000;
公共建筑内部最小高度=15000;
私人国际航空公司;
公共高度
{
得到
{
返回当前高度;
}
设置
{
_当前高度=数值;
}
}
private bool airplaneIsDead=假;
//定义委托类型
公共代表void GoneTooHigh(字符串消息);
公共委托void GoneTooLow(字符串消息);
//定义上述委托类型的成员变量
私家GoneToohightMaxHeightViolationList;
私人GoneTooLow MinHeightVioloationList;
//使用helper方法将成员添加到调用列表中
公共空间OnGoneTooHigh(GoneTooHigh客户法)
{
MaxHeightViolationList=clientMethod;
}
公共void OnGoneTooLow(GoneTooLow客户端方法)
{
MinHeightVioloationList=clientMethod;
}
无效\u计时器\u已过(对象发送器,ElapsedEventArgs e)
{
if(当前高度<最大允许高度)
{
_当前高度++;
}
其他的
{
_当前高度--;
}
}
}
}您应该使用System.Timers.Timer,原因如下:
基本上,使用Winforms定时器控件,您将任由Winforms代码的其余部分支配,它不一定会给您相等的间隔。由于这是一个控制台应用程序,Windows Forms定时器不会真正工作 我会选择线程计时器
请注意,控制台应用程序中的多线程计时可能有点。。。傻。在另一个线程完成之前,您的主线程将处于一个无限循环中,几乎什么也不做 我打算将此应用程序制作为WPF或Silverlight。我试图实现良好的模型-视图分离。请参阅我的后续问题以获得进一步澄清,我不一定关心相等的时间间隔。我的目标是“模拟”一个“海拔上升或下降的速率”。我的目标是实现增加或减少的延迟。请参阅我的后续问题以获得进一步澄清,
class Airplane
{
public Airplane()
{
_currentAltitude = 0;
Timer _timer = new Timer();
_timer.Start();
Console.WriteLine("airplane started");
Console.ReadKey();
}
public const int MAXALLOWABLEHEIGHT = 30000;
public const int MINALLOWABLEHEIGHT = 15000;
private int _currentAltitude;
public int CurrentAltitude
{
get
{
return _currentAltitude;
}
set
{
_currentAltitude = value;
}
}
private bool airplaneIsDead = false;
// Define the delegate types
public delegate void GoneTooHigh(string msg);
public delegate void GoneTooLow(string msg);
// Define member variables of the above delegate types
private GoneTooHigh MaxHeightViolationList;
private GoneTooLow MinHeightVioloationList;
// Add members to the invocation lists using helper methods
public void OnGoneTooHigh(GoneTooHigh clientMethod)
{
MaxHeightViolationList = clientMethod;
}
public void OnGoneTooLow(GoneTooLow clientMethod)
{
MinHeightVioloationList = clientMethod;
}
void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (_currentAltitude < MAXALLOWABLEHEIGHT)
{
_currentAltitude++;
}
else
{
_currentAltitude--;
}
}
}