C# 根据什么逻辑,实例构造函数应该访问静态字段

C# 根据什么逻辑,实例构造函数应该访问静态字段,c#,constructor,C#,Constructor,我想知道为什么实例构造函数可以访问静态字段?如果我通过静态构造函数初始化静态字段,并且错误地再次通过实例构造函数初始化它们,那么第二次初始化将覆盖第一次初始化。通过实例构造函数访问它们背后的想法是什么?(请看下面的简单程序以了解我的观点) 例如,当静态字段包含类的实例计数器时,构造函数可能希望访问静态成员。您可能希望类成员获取、保留(在一个非静态字段中)并递增这个将是静态的计数器。在将来的任何时候,该实例都将具有自己的唯一标识符 例如: public class Employee {

我想知道为什么实例构造函数可以访问静态字段?如果我通过静态构造函数初始化静态字段,并且错误地再次通过实例构造函数初始化它们,那么第二次初始化将覆盖第一次初始化。通过实例构造函数访问它们背后的想法是什么?(请看下面的简单程序以了解我的观点)


例如,当静态字段包含类的实例计数器时,构造函数可能希望访问静态成员。您可能希望类成员获取、保留(在一个非静态字段中)并递增这个将是静态的计数器。在将来的任何时候,该实例都将具有自己的唯一标识符

例如:

public class Employee {
      static int NextEmployeeId;  // a counter across all employees

      public int EmployeeId;  // this instance's employee id
      public string Name;     // this instance's name.

      static Employee() {
          Employee.NextEmployeeId = 1;  // first employee gets 1.
      }

      public Employee(string Name) {
           this.Name = Name;
           this.EmployeeId = Employee.NextEmployeeId++;  // take an id and increment for the next employee
      }

}

静态字段可以从任何地方访问,即使是从构造函数,或者甚至是从Main/other类。这样做的目的是,整个应用程序只有一个静态属性/字段单例

public class AClass()
{
   public static float staticField;
   public float field;
   public AClass()
   {
       staticField = 5;
       field = 6;
   }
   static AClass()
   {
       staticField = 7;
   }
}

public int Main()
{
     float initially = AClass.staticField; // initially this staticField is 7.
     AClass aclass = new AClass(); // instantiating AClass
     float localfield = aclass.field; // this field does not affect anyone. It is 6
     float newStaticField = AClass.staticField; // Due to previous instantiation, the value is now 5.

}
我同意你的观点,在你的例子中,这是不好的。为什么?因为为什么要更改Pi的值,因为它已经被确定和固定,所以没有理由在构造函数中更改Pi的值

您可能需要知道如何设计类,并了解为什么首先需要静态字段。下面是一个类的示例,该类正确地使用了一个静态字段(有点…例如,因为密钥应该是隐藏的。这只是为了向您展示静态字段是如何有用和正确的。):

这里的目的是,如果您实例化一个NewEncryptionClass,您将希望保存密钥,以便下次进行加密时,您将始终使用最新的密钥,而不必每次都指定它。例如:

public int Main()
{
     string initialkey = NewEncryptionClass.Key;
     string result1 = new EncryptionClass().Encrypt("encryptThis"); // using key1

     // let's change the key
     string result2 = new EncryptionClass("key2").Encrypt("encryptThat"); // using key2
     string result3 = new EncryptionClass().Encrypt("encryptOther"); // still using key2


}

当然,如果我想永远保留最新的密钥,如果不是,那么这个类设计是错误的,你需要为你的目的重写它。

使用
const
,这使它无法写入。丹尼尔,这不是我问题的重点。还有其他方法可以使我的静态场不可写。我关心的是为什么会提供此功能。。??比如在哪个例子中,实例构造函数需要使用静态字段吗?先生,你能用一个例子来解释你的答案吗。??只是为了确保我理解了你试图解释的同一件事:P
public class NewEncryptionClass()
{
      public static string Key;
      public NewEncryptionClass()
      {

      }
      public NewEncryptionClass(string newKey)
      {
           Key = newKey; // store the key and keep it forever
      }
      static NewEncryptionClass()
      {
           Key = "key1"; // initially key is "key1"
      }
      public string Encrypt(string str)
      {
          string result = string.Empty;
          result =  "adlasdjalskd" + Key + "ajlajfalkjfa" + str; // do the Encryption, I just made up
          return result
      }
}
public int Main()
{
     string initialkey = NewEncryptionClass.Key;
     string result1 = new EncryptionClass().Encrypt("encryptThis"); // using key1

     // let's change the key
     string result2 = new EncryptionClass("key2").Encrypt("encryptThat"); // using key2
     string result3 = new EncryptionClass().Encrypt("encryptOther"); // still using key2


}