A private constructor is a special instance constructor. It is generally used in classes that contain static members only. If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class. For example:

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 = ;
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();
}
}
// Output: New count: 101

C# empty private constructor的更多相关文章

  1. Effective Java 03 Enforce the singleton property with a private constructor or an enum type

    Principle When implement the singleton pattern please decorate the INSTANCE field with "static ...

  2. Effective Java 04 Enforce noninstantiability with a private constructor

    A class can be made noninstantiable by including a private constructor. // Noninstantiable utility c ...

  3. Effective Java Item4:Enforce noninstantiability with a private constructor

    Item4:Enforce noninstantiability with a private constructor 通过构造私有化,禁止对象被实例化. public class UtilClass ...

  4. Effective Java Item3:Enforce the singleton property with a private constructor or an enum type

    Item3:Enforce the singleton property with a private constructor or an enum type 采用枚举类型(ENUM)实现单例模式. ...

  5. Java之创建对象>4.Enforce noninstantiability with a private constructor

    如果你定义的类仅仅是包含了一些静态的方法和静态的字段,这些类一般是一些工具类,这些一般是设计为不能被实例化的. 1. Attempting to enforce noninstantiability ...

  6. add a private constructor to hide the implicit public one(Utility classes should not have public constructors)

    sonarlint提示add a private constructor to hide the implicit public one Utility classes should not have ...

  7. [CareerCup] 14.1 Private Constructor 私有构建函数

    14.1 In terms of inheritance, what is the effect of keeping a constructor private? 这道题问我们用继承特性时,如果建立 ...

  8. Java之创建对象>3.Enforce the singleton property with a private constructor or an enum type

     1. 通过一个公开的字段来获取单例 // Singleton with public final field public class Elvis { public static final Elv ...

  9. hdu 1041 (OO approach, private constructor to prevent instantiation, sprintf) 分类: hdoj 2015-06-17 15:57 25人阅读 评论(0) 收藏

    a problem where OO seems more natural to me, implementing a utility class not instantiable. how to p ...

随机推荐

  1. VS2010 打开 VS2012 的项目

    用 VS2010 打开 VS2012 项目,只需两步. 1. 修改解决方案文件(*.sln) 使用记事本打开 *.sln 文件,将里面的 Microsoft Visual Studio Solutio ...

  2. windows 2003 企业版 下载地址+序列号

    迅雷地址: thunder://QUFodHRwOi8vcy5zYWZlNS5jb20vV2luZG93c1NlcnZlcjIwMDNTUDJFbnRlcnByaXNlRWRpdGlvbi5pc29a ...

  3. 在Asp.Net MVC中用Ajax回调后台方法

    在Asp.Net MVC中用Ajax回调后台方法基本格式: var operData = ...; //传递的参数(action中定义的) var type = ...; //传递的参数(action ...

  4. 通过SharePoint Designer对SharePoint 2010的Master Page进行自定制

    1:需要在对应的SiteCollection 和 Site 中开启Publishing的服务 2:在Designer中创建自己的Master Page,进行对原始v4.master代码进行复制,和修改 ...

  5. Android中常用的五种数据存储方式

    第一种: 使用SharedPreferences存储数据 适用范围: 保存少量的数据,且这些数据的格式非常简单:字符串型.基本类型的值.比如应用程序的各种配置信息(如是否打开音效.是否使用震动效果.小 ...

  6. 【java 获取数据库信息】获取MySQL或其他数据库的详细信息

    1.首先是 通过数据库获取数据表的详细列信息 package com.sxd.mysqlInfo.test; import java.sql.Connection; import java.sql.D ...

  7. javascript引擎工作原理

    1. 什么是JavaScript解析引擎? 简单地说,JavaScript解析引擎就是能够“读懂”JavaScript代码,并准确地给出代码运行结果的一段程序.比方说,当你写了 var a = 1 + ...

  8. transform(变形)和transform-origin(变形原点)

    转载请说明出处,原文地址http://blog.sina.com.cn/s/blog_780a942701014xl8.html transform(变形)和transform-origin(变形原点 ...

  9. SQL ISNULL 函数

    sql 中 NULL 值的处理:微软的 ISNULL() 函数用于规定如何处理 NULL 值.NVL(), IFNULL() 和 COALESCE() 函数也可以达到相同的结果.语法ISNULL ( ...

  10. #include<>与#include""

    对于头文件的包含,使用“<>”时,系统会到默认目录(编译器及环境变量,工程文件定义的头文件寻找目录,包括Qt安装的include目录)查找要包含的文件,这是标准方式: 用双引号时,系统先到 ...