当前位置: 移动技术网 > IT编程>开发语言>c# > 解析C#中的私有构造函数和静态构造函数

解析C#中的私有构造函数和静态构造函数

2019年07月18日  | 移动技术网IT编程  | 我要评论

私有构造函数
私有构造函数是一种特殊的实例构造函数。它通常用在只包含静态成员的类中。如果类具有一个或多个私有构造函数而没有公共构造函数,则其他类(除嵌套类外)无法创建该类的实例。例如:

class nlog
{
  // private constructor:
  private nlog() { }

  public static double e = math.e; //2.71828...
}

声明空构造函数可阻止自动生成默认构造函数。注意,如果您不对构造函数使用访问修饰符,则在默认情况下它仍为私有构造函数。但是,通常显式地使用 private 修饰符来清楚地表明该类不能被实例化。
当没有实例字段或实例方法(如 math 类)时或者当调用方法以获得类的实例时,私有构造函数可用于阻止创建类的实例。如果类中的所有方法都是静态的,可考虑使整个类成为静态的。

下面是使用私有构造函数的类的示例。

public class counter
{
  private counter() { }
  public static int currentcount;
  public static int incrementcount()
  {
    return ++currentcount;
  }
}

class testcounter
{
  static void main()
  {
    // if you uncomment the following statement, it will generate
    // an error because the constructor is inaccessible:
    // counter acounter = new counter();  // error

    counter.currentcount = 100;
    counter.incrementcount();
    console.writeline("new count: {0}", counter.currentcount);

    // keep the console window open in debug mode.
    console.writeline("press any key to exit.");
    console.readkey();
  }
}

输出:

new count: 101

注意,如果您取消注释该示例中的以下语句,它将生成一个错误,因为该构造函数受其保护级别的限制而不可访问:

// counter acounter = new counter();  // error

静态构造函数
静态构造函数用于初始化任何静态数据,或用于执行仅需执行一次的特定操作。在创建第一个实例或引用任何静态成员之前,将自动调用静态构造函数。

class simpleclass
{
  // static variable that must be initialized at run time.
  static readonly long baseline;

  // static constructor is called at most one time, before any
  // instance constructor is invoked or member is accessed.
  static simpleclass()
  {
    baseline = datetime.now.ticks;
  }
}

静态构造函数具有以下特点:

  • 静态构造函数既没有访问修饰符,也没有参数。
  • 在创建第一个实例或引用任何静态成员之前,将自动调用静态构造函数来初始化类。
  • 无法直接调用静态构造函数。
  • 在程序中,用户无法控制何时执行静态构造函数。

静态构造函数的典型用途是:当类使用日志文件时,将使用这种构造函数向日志文件中写入项。

静态构造函数在为非托管代码创建包装类时也很有用,此时该构造函数可以调用 loadlibrary 方法。
如果静态构造函数引发异常,运行时将不会再次调用该构造函数,并且在程序运行所在的应用程序域的生存期内,类型将保持未初始化。
在此示例中,类 bus 有一个静态构造函数。创建 bus 的第一个实例(bus1)时,将调用该静态构造函数来初始化该类。输出示例验证了即使创建 bus 的两个实例,该静态构造函数也仅运行一次,并且在实例构造函数运行之前运行。

 public class bus
 {
   // static variable used by all bus instances.
   // represents the time the first bus of the day starts its route.
   protected static readonly datetime globalstarttime;

   // property for the number of each bus.
   protected int routenumber { get; set; }

   // static constructor to initialize the static variable.
   // it is invoked before the first instance constructor is run.
   static bus()
   {
     globalstarttime = datetime.now;

     // the following statement produces the first line of output, 
     // and the line occurs only once.
     console.writeline("static constructor sets global start time to {0}",
       globalstarttime.tolongtimestring());
   }

   // instance constructor.
   public bus(int routenum)
   {
     routenumber = routenum;
     console.writeline("bus #{0} is created.", routenumber);
   }

   // instance method.
   public void drive()
   {
     timespan elapsedtime = datetime.now - globalstarttime;

     // for demonstration purposes we treat milliseconds as minutes to simulate
     // actual bus times. do not do this in your actual bus schedule program!
     console.writeline("{0} is starting its route {1:n2} minutes after global start time {2}.",
                 this.routenumber,
                 elapsedtime.totalmilliseconds,
                 globalstarttime.toshorttimestring());
   }
 }

 class testbus
 {
   static void main()
   {
     // the creation of this instance activates the static constructor.
     bus bus1 = new bus(71);

     // create a second bus.
     bus bus2 = new bus(72);

     // send bus1 on its way.
     bus1.drive();

     // wait for bus2 to warm up.
     system.threading.thread.sleep(25);

     // send bus2 on its way.
     bus2.drive();

     // keep the console window open in debug mode.
     system.console.writeline("press any key to exit.");
     system.console.readkey();
   }
 }

输出:

   static constructor sets global start time to 3:57:08 pm.
   bus #71 is created.
   bus #72 is created.
   71 is starting its route 6.00 minutes after global start time 3:57 pm.
   72 is starting its route 31.00 minutes after global start time 3:57 pm.   

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网