最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解析C#中的私有構(gòu)造函數(shù)和靜態(tài)構(gòu)造函數(shù)

 更新時(shí)間:2016年01月30日 17:29:31   投稿:goldensun  
這篇文章主要介紹了C#中的私有構(gòu)造函數(shù)和靜態(tài)構(gòu)造函數(shù),是C#入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下

私有構(gòu)造函數(shù)
私有構(gòu)造函數(shù)是一種特殊的實(shí)例構(gòu)造函數(shù)。它通常用在只包含靜態(tài)成員的類(lèi)中。如果類(lèi)具有一個(gè)或多個(gè)私有構(gòu)造函數(shù)而沒(méi)有公共構(gòu)造函數(shù),則其他類(lèi)(除嵌套類(lèi)外)無(wú)法創(chuàng)建該類(lèi)的實(shí)例。例如:

class NLog
{
  // Private Constructor:
  private NLog() { }

  public static double e = Math.E; //2.71828...
}

聲明空構(gòu)造函數(shù)可阻止自動(dòng)生成默認(rèn)構(gòu)造函數(shù)。注意,如果您不對(duì)構(gòu)造函數(shù)使用訪(fǎng)問(wèn)修飾符,則在默認(rèn)情況下它仍為私有構(gòu)造函數(shù)。但是,通常顯式地使用 private 修飾符來(lái)清楚地表明該類(lèi)不能被實(shí)例化。
當(dāng)沒(méi)有實(shí)例字段或?qū)嵗椒ǎㄈ?Math 類(lèi))時(shí)或者當(dāng)調(diào)用方法以獲得類(lèi)的實(shí)例時(shí),私有構(gòu)造函數(shù)可用于阻止創(chuàng)建類(lèi)的實(shí)例。如果類(lèi)中的所有方法都是靜態(tài)的,可考慮使整個(gè)類(lèi)成為靜態(tài)的。

下面是使用私有構(gòu)造函數(shù)的類(lèi)的示例。

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

注意,如果您取消注釋該示例中的以下語(yǔ)句,它將生成一個(gè)錯(cuò)誤,因?yàn)樵摌?gòu)造函數(shù)受其保護(hù)級(jí)別的限制而不可訪(fǎng)問(wèn):

// Counter aCounter = new Counter();  // Error

靜態(tài)構(gòu)造函數(shù)
靜態(tài)構(gòu)造函數(shù)用于初始化任何靜態(tài)數(shù)據(jù),或用于執(zhí)行僅需執(zhí)行一次的特定操作。在創(chuàng)建第一個(gè)實(shí)例或引用任何靜態(tài)成員之前,將自動(dòng)調(diào)用靜態(tài)構(gòu)造函數(shù)。

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;
  }
}

靜態(tài)構(gòu)造函數(shù)具有以下特點(diǎn):

  • 靜態(tài)構(gòu)造函數(shù)既沒(méi)有訪(fǎng)問(wèn)修飾符,也沒(méi)有參數(shù)。
  • 在創(chuàng)建第一個(gè)實(shí)例或引用任何靜態(tài)成員之前,將自動(dòng)調(diào)用靜態(tài)構(gòu)造函數(shù)來(lái)初始化類(lèi)。
  • 無(wú)法直接調(diào)用靜態(tài)構(gòu)造函數(shù)。
  • 在程序中,用戶(hù)無(wú)法控制何時(shí)執(zhí)行靜態(tài)構(gòu)造函數(shù)。

靜態(tài)構(gòu)造函數(shù)的典型用途是:當(dāng)類(lèi)使用日志文件時(shí),將使用這種構(gòu)造函數(shù)向日志文件中寫(xiě)入項(xiàng)。

靜態(tài)構(gòu)造函數(shù)在為非托管代碼創(chuàng)建包裝類(lèi)時(shí)也很有用,此時(shí)該構(gòu)造函數(shù)可以調(diào)用 LoadLibrary 方法。
如果靜態(tài)構(gòu)造函數(shù)引發(fā)異常,運(yùn)行時(shí)將不會(huì)再次調(diào)用該構(gòu)造函數(shù),并且在程序運(yùn)行所在的應(yīng)用程序域的生存期內(nèi),類(lèi)型將保持未初始化。
在此示例中,類(lèi) Bus 有一個(gè)靜態(tài)構(gòu)造函數(shù)。創(chuàng)建 Bus 的第一個(gè)實(shí)例(bus1)時(shí),將調(diào)用該靜態(tài)構(gòu)造函數(shù)來(lái)初始化該類(lèi)。輸出示例驗(yàn)證了即使創(chuàng)建 Bus 的兩個(gè)實(shí)例,該靜態(tài)構(gòu)造函數(shù)也僅運(yùn)行一次,并且在實(shí)例構(gòu)造函數(shù)運(yùn)行之前運(yùn)行。

 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.   

相關(guān)文章

  • C#線(xiàn)性漸變畫(huà)刷LinearGradientBrush用法實(shí)例

    C#線(xiàn)性漸變畫(huà)刷LinearGradientBrush用法實(shí)例

    這篇文章主要介紹了C#線(xiàn)性漸變畫(huà)刷LinearGradientBrush用法,實(shí)例分析了線(xiàn)性漸變畫(huà)刷LinearGradientBrush的相關(guān)使用技巧,需要的朋友可以參考下
    2015-06-06
  • C#導(dǎo)入導(dǎo)出EXCEL文件的代碼實(shí)例

    C#導(dǎo)入導(dǎo)出EXCEL文件的代碼實(shí)例

    這篇文章主要介紹了C#導(dǎo)入導(dǎo)出EXCEL文件代碼實(shí)例,代碼的流程和方法都很詳細(xì),需要的朋友可以參考下
    2014-04-04
  • C#項(xiàng)目中跨文件調(diào)用公共類(lèi)的實(shí)例方法

    C#項(xiàng)目中跨文件調(diào)用公共類(lèi)的實(shí)例方法

    在本篇文章里小編給大家整理的是關(guān)于C#項(xiàng)目中如何跨文件調(diào)用公共類(lèi)的知識(shí)點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。
    2019-08-08
  • C#任務(wù)并行Parellel.For和Parallel.ForEach

    C#任務(wù)并行Parellel.For和Parallel.ForEach

    這篇文章介紹了C#任務(wù)并行Parellel.For和Parallel.ForEach的用法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • c++指針使用形參改變實(shí)參的方法

    c++指針使用形參改變實(shí)參的方法

    下面小編就為大家?guī)?lái)一篇c++指針使用形參改變實(shí)參的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-03-03
  • C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法

    C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法

    這篇文章主要介紹了C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法,涉及C#文件傳輸?shù)募记?具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-08-08
  • c#基礎(chǔ)知識(shí)---委托,匿名函數(shù),lambda

    c#基礎(chǔ)知識(shí)---委托,匿名函數(shù),lambda

    這篇文章主要介紹了c# 委托,匿名函數(shù),lambda的相關(guān)知識(shí),文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • 在 Visual Studio 中查看反匯編代碼

    在 Visual Studio 中查看反匯編代碼

    這篇文章主要介紹了在 Visual Studio 中查看反匯編代碼的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • c# 通過(guò)WinAPI播放PCM聲音

    c# 通過(guò)WinAPI播放PCM聲音

    這篇文章主要介紹了c# 通過(guò)WinAPI播放PCM聲音的方法,幫助大家更好的理解和使用c#編程語(yǔ)言,感興趣的朋友可以了解下
    2020-12-12
  • C#微信公眾號(hào)開(kāi)發(fā)之消息處理

    C#微信公眾號(hào)開(kāi)發(fā)之消息處理

    這篇文章介紹了C#微信公眾號(hào)開(kāi)發(fā)之消息處理,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06

最新評(píng)論

苏尼特右旗| 辽宁省| 锦州市| 基隆市| 正蓝旗| 崇文区| 涿鹿县| 通城县| 万安县| 辛集市| 保靖县| 池州市| 西峡县| 平安县| 克东县| 德州市| 肇东市| 永丰县| 宁海县| 泗洪县| 峡江县| 武陟县| 兴安盟| 睢宁县| 东乌珠穆沁旗| 虞城县| 荔浦县| 天峻县| 文水县| 冀州市| 周至县| 中江县| 澎湖县| 宁都县| 珠海市| 仪征市| 壤塘县| 怀安县| 沧源| 炉霍县| 若尔盖县|