繁体   English   中英

Static class with instance variable works until I intoduce another variable into the static class that is static

[英]Static class with instance variable works until I intoduce another variable into the static class that is static

static class Program
{
     static void Main()
     {
         DatabaseQueueLoader loader = new DatabaseQueueLoader();
         MFilesClient client = new MFilesClient();
         client.Connect();
     }
}


static class Program
{
     static void Main()
     {
         DatabaseQueueLoader loader = new DatabaseQueueLoader();
         private static NLogger logger = NLogger.UniqueInstance;
         MFilesClient client = new MFilesClient();
         client.Connect();
     }
}

顶部的代码段有效,但底部的代码段无效。 我的理解是,在 static class 中,需要拥有其所有成员 static。 这是我继承的代码,我正在努力全面了解正在发生的事情。 我不明白为什么这个顶级版本的代码可以工作。 这可能有助于我弄清楚为什么当我介绍 static 成员时,它不再起作用了。 我还想知道为什么 static 成员会破坏它。 我可以通过多种方式解决此问题,但希望获得更深入的了解。

您不能在方法中使用 private 关键字或 static。 您只能在 class 中使用它,这就是为什么底部不工作的原因。

“私有 static NLogger 记录器..”不正确。 在方法内部时,变量本身不需要标记为 static。 如果 NLogger class 是 static,则不需要为 NLogger 声明变量,您可以直接引用 class 中的方法。 如果 NLogger 不是 static 而是需要声明的 object ,则从该变量中删除 static 并正常声明。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM