C#私有構(gòu)造函數(shù)使用示例
聲明空構(gòu)造函數(shù)可阻止自動生成默認構(gòu)造函數(shù)。注意,如果您不對構(gòu)造函數(shù)使用訪問修飾符,則在默認情況下它仍為私有構(gòu)造函數(shù)。但是,通常顯式地使用 private 修飾符來清楚地表明該類不能被實例化。
示例代碼:
public class PrivateConClass
{
private static PrivateConClass pcc;
private PrivateConClass()
{
Console.WriteLine("This private constructure function. So you cannot create an instance of this class.");
}
public static PrivateConClass CreatePcc()
{
pcc = new PrivateConClass();
return pcc;
}
public static void ShowStaticMethod()
{
Console.WriteLine("This is a static method. Just be called by Class name.");
}
public void ShowMethod()
{
Console.WriteLine("This is a Nonstatic method. Just be called by private static instance pcc.");
}
}
class Program
{
static void Main(string[] args)
{
PrivateConClass pcc = PrivateConClass.CreatePcc();
pcc.ShowMethod();
PrivateConClass.ShowStaticMethod();
}
}
- C#構(gòu)造函數(shù)詳解
- C# 構(gòu)造函數(shù)如何調(diào)用虛方法
- 淺談C# 構(gòu)造方法(函數(shù))
- C#類繼承中構(gòu)造函數(shù)的執(zhí)行序列示例詳解
- C#中構(gòu)造函數(shù)和析構(gòu)函數(shù)用法實例詳解
- 詳解C#編程中構(gòu)造函數(shù)的使用
- C#靜態(tài)構(gòu)造函數(shù)用法實例分析
- C#中靜態(tài)構(gòu)造函數(shù)的幾點說明介紹
- c#只讀字段和常量的區(qū)別,以及靜態(tài)構(gòu)造函數(shù)的使用實例
- C# 靜態(tài)構(gòu)造函數(shù)使用總結(jié)
- C#構(gòu)造函數(shù)在基類和父類中的執(zhí)行順序
相關(guān)文章
淺談C#中HttpWebRequest與HttpWebResponse的使用方法
本篇文章主要介紹了淺談C#中HttpWebRequest與HttpWebResponse的使用方法,具有一定的參考價值,有興趣的可以了解一下。2017-01-01C#進階系列 WebApi身份認證解決方案推薦:Basic基礎(chǔ)認證
下面小編就為大家?guī)硪黄狢#進階系列 WebApi身份認證解決方案推薦:Basic基礎(chǔ)認證。小編覺得挺不錯的,現(xiàn)在分享給大家。給大家一個參考。一起跟隨小編過來看看吧2016-03-03