传递数据时,我调用递归并用完堆栈,原因如下:
类有需要传递给类的Controller_MainForm变量public int cordX, cordY;Raket
这是我正在尝试做的事情:
class Raket
{
Controller_MainForm control;
public Raket()
{
control = new Controller_MainForm();
x = control.cordX;
y = control.cordY;
}
但是在类方法中创建Controller_MainForm了类的实例Model
public partial class Controller_MainForm : Form
{
View view;
Model model;
public int cordX, cordY;
Thread modelGo;
public Controller_MainForm() : this(850) { }
public Controller_MainForm(int sizeField) : this(sizeField, 1) { }
public Controller_MainForm(int sizeField, int amountRaket) : this(sizeField, amountRaket, 1) { }
public Controller_MainForm(int sizeField, int amountRaket, int amountAero) : this(sizeField, amountRaket, amountAero, 40) { }
public Controller_MainForm(int sizeField, int amountRaket, int amountAero, int speedGame)
{
InitializeComponent();
model = new Model(sizeField, amountAero, amountRaket, speedGame);
view = new View(model);
this.Controls.Add(view);
cordX = Convert.ToInt32(textBox1.Text);
cordY = Convert.ToInt32(textBox2.Text);
}
private void Start_Click(object sender, EventArgs e)
{
if (model.gameStatus == GameStatus.stoping)
{
model.gameStatus = GameStatus.playing;
modelGo = new Thread(model.Go);
modelGo.Start();
view.Invalidate();
}
}
private void Controller_MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (modelGo != null)
{
model.gameStatus = GameStatus.stoping;
modelGo.Abort();
}
DialogResult dr = MessageBox.Show("Закрыть приложение?" , "Radionavig_24", MessageBoxButtons.YesNoCancel);
if (dr == DialogResult.Yes)
e.Cancel = false;
else
e.Cancel = true;
}
}
在模型类中,方法:
class Model
{
int sizeField;
int amountRaket;
int amountAero;
public int speedGame;
public GameStatus gameStatus;
public Raket raket;
public Model(int sizeField, int amountRaket, int amountAero, int speedGame)
{
this.sizeField = sizeField;
this.amountAero = amountAero;
this.amountRaket = amountRaket;
this.speedGame = speedGame;
raket = new Raket();
gameStatus = GameStatus.stoping;
}
public void Go()
{
while (gameStatus == GameStatus.playing)
{
Thread.Sleep(speedGame);
raket.Run();
}
}
}
并再次创建一个实例,new Raket()如此循环。如何在不递归的情况下传递数据?Raket()Controller_MainForm
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] arg)
{
Controller_MainForm cm;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
switch (arg.Length)
{
case 0: cm = new Controller_MainForm(); break;
case 1: cm = new Controller_MainForm(Convert.ToInt32(arg[0])); break;
case 2: cm = new Controller_MainForm(Convert.ToInt32(arg[0]), Convert.ToInt32(arg[1])); break;
case 3: cm = new Controller_MainForm(Convert.ToInt32(arg[0]), Convert.ToInt32(arg[1]), Convert.ToInt32(arg[2])); break;
case 4: cm = new Controller_MainForm(Convert.ToInt32(arg[0]), Convert.ToInt32(arg[1]), Convert.ToInt32(arg[2]), Convert.ToInt32(arg[3])); break;
default: cm = new Controller_MainForm(); break;
}
Application.Run(cm);
}
}
我不太了解你的类的依赖关系和层次结构,但你可以像这样实现将对象传递给构造函数:
火箭级:
Controller_MainForm 类:
模型类:
您的构造函数
Controller_MainForm()与列表中显示的构造函数不匹配。因此,我几乎是随意写的。如果有什么不对,请写在评论中。