gpt4 book ai didi

c# - 这些静态变量的可能替代方案是什么?

转载 作者:太空狗 更新时间:2023-10-30 00:57:35 25 4
gpt4 key购买 nike

我已经创建了一个简单的 GUI 引擎,我打算在游戏中使用它。我遇到的问题是理解如何实例化一个将在多个堆栈帧中访问的类而不是静态的(人们已经非常清楚地表明静态变量只是邪恶的)。

下面是一个代码示例:

class MyGame
{
class InputEngine
{
internal void DoInput()
{
if (Keys["F1"].IsPressed)
{
// Create a window using the gui engine
}
}
}
class GuiEngine
{
internal void Update() { }
internal void Draw() { }
}
private GuiEngine engine;
private InputEngine input;

internal MyGame()
{
this.input = new InputEngine();
this.engine = new GuiEngine();
}

internal void Update()
{
this.engine.Update();
this.input.DoInput();
}
internal void Draw()
{
this.engine.Draw();
}
}

我如何才能不仅从输入示例访问 gui 引​​擎实例,而且从其他几十个地方访问它而不使其成为静态的。 (我真的不想将它作为参数传递)。

最佳答案

您的问题的解决方案可能是重新设计。需要的时候出问题了

access the gui engine instance not only from the input example, but from tens of other places

这表明很多依赖项可能是错误的。

关于c# - 这些静态变量的可能替代方案是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4773073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com