我知道有很多关于这个问题的帖子,但到目前为止我还没有找到一个能直接帮助我的情况。 我需要从静态和非静态方法中访问类的成员。 但是,如果成员是非静态的,我似乎无法从静态方法中访问它们。
public class SomeCoolClass
{
public string Summary = "I'm telling you";
public void DoSomeMethod()
{
string myInterval = Summary + " this is what happened!";
}
public static void DoSomeOtherMethod()
{
string myInterval = Summary + " it didn't happen!";
}
}
public class MyMainClass
{
SomeCoolClass myCool = new SomeCoolClass();
myCool.DoSomeMethod();
SomeCoolClass.DoSomeOtherMethod();
}
您建议我如何从这两种方法中获取摘要?
Summary
设为常量吗?你可以将其标记为public const string Summary
,这样你就可以从两个地方访问它了。 - Justin Skiles