C#类位于文件夹中,需要调用另一个文件夹中的类
我有一个名为C#类位于文件夹中,需要调用另一个文件夹中的类,c#,class,reference,xna,directory,C#,Class,Reference,Xna,Directory,我有一个名为StartUp的文件夹,它保存了开始的类,并在程序中首先加载。我想访问另一个名为total的文件夹,它包含将在整个游戏中使用的类。我想从总体上获取一个名为鼠标的类,并将其转移到启动中的任何其他类上 编辑:2013年7月12日10:00 这是文件和文件夹,例如,Mouses.cs有一个public int thing=1我希望它移动到TitleScreen.cs,我该如何允许TitleScreen.cs查看/编辑该int 我可以访问Game1.cs中的所有类,方法是调用那里的文件夹,然
StartUp
的文件夹,它保存了开始的类,并在程序中首先加载。我想访问另一个名为total
的文件夹,它包含将在整个游戏中使用的类。我想从总体上
获取一个名为鼠标
的类,并将其转移到启动
中的任何其他类上
编辑:2013年7月12日10:00
这是文件和文件夹,例如,Mouses.cs
有一个public int thing=1
我希望它移动到TitleScreen.cs
,我该如何允许TitleScreen.cs
查看/编辑该int
我可以访问Game1.cs
中的所有类,方法是调用那里的文件夹,然后调用类(例如total.Mouses.ect=1;
),反之亦然
总体.Mouses.cs
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Lover__Regret
{
class Mouses
{
public int thing = 1;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Love__Regret.StartUp
{
class TitleScreen
{
public static void Update(GameTime gameTime)
{
//Want to call thing here
}
}
}
StartUp.TitleScreen.cs
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Lover__Regret
{
class Mouses
{
public int thing = 1;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Love__Regret.StartUp
{
class TitleScreen
{
public static void Update(GameTime gameTime)
{
//Want to call thing here
}
}
}
嗯,
鼠标中的thing
字段不是静态字段。这意味着您需要在TitleScreen
中有一个对mouse
的实例引用,才能访问它
您说过在Game1.cs
中已经有了实例的引用。因此,我建议做以下几点:
TitleScreen
中的Update
方法不应是静态的。您需要更新特定的标题屏幕
实例。因此,删除static关键字
为TitleScreen
创建一个构造函数,该构造函数接受mouse
的一个实例,并创建一个字段来存储该实例:
私人鼠标(mouseHandler),
公共标题屏幕(Mouses mouseHandler)
{
_mouseHandler=mouseHandler;
}
然后,在Update
方法中调用如下内容:
Console.WriteLine(_mouseHandler.thing);//或者对鼠标中的字段执行任何操作
现在,当您在Game1.cs
中构建TitleScreen
时,通过传递一个鼠标
实例来创建它:
//创建鼠标实例(例如:mouses mouses=new mouses())
标题屏幕=新标题屏幕(鼠标)
大多数编程不是通过静态类/方法完成的(这将是过程性编程)。相反,您需要创建类的实例 你说的“上一节课,把它转过来”是什么意思?一些代码可能有助于说明这个想法。@CodeCaster我将更新帖子。感谢您的帮助,我希望绕过主类(Game1.cs
),而不必使用它。由于我知道如何引用类,但希望使用最简单的代码,这意味着我必须对每个类都做同样的操作,比如ie.学分
,选项
,标题屏幕
以及任何其他需要鼠标
类的类。跳过主类可以节省大约20行代码,但我还是尝试了最少的代码。我已经解释了为什么我无法访问另一个文件夹总体上
,程序中有一个bug。我不知道它是什么,但创建一个新项目,创建两个文件夹,并在每个文件夹中添加一个类,我只需执行以下操作即可访问另一个类:private-all.Mouses然后可以访问鼠标的信息并随意更改。我不知道另一个项目出了什么问题。嗯,你最好尝试编写可维护的代码,即使它“只是”多出20行代码。我的意思是你的代码不应该都是静态的。如果您不想“弄乱”实例,至少要使用单例模式:)