【发布时间】:2015-04-02 17:01:24
【问题描述】:
我在 Unity3D 中使用 C# 进行编程,并在学习对象池时遇到了类似的代码:
public class MyClass : Object
{
public static MyClass current;
void Awake()
{
current = this;
}
public void SomeMethod()
{
}
}
public class Other
{
void AnotherMethod()
{
MyClass.current.SomeMethod();
}
}
现在 MyClass 类是非静态的,但是对它的实例 'this' 的引用 'current' 是(静态的)。 我可以调用公共非静态方法并访问所有其他公共非静态变量,方法是使用其他类中对“this”实例的静态引用“current”。
但如果我从未创建类 MyClass 的实例,那么静态引用指向什么?
【问题讨论】:
-
你没有找到比
Class更好的名字吗? -
@kyle 对不起!我的错。我将其编辑为 MyClass。