声明空构造函数可阻止自动生成默认构造函数。注意,如果您不对构造函数使用访问修饰符,则在默认情况下它仍为私有构造函数。但是,通常显式地使用 private 修饰符来清楚地表明该类不能被实例化。
示例代码:
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();
}
}
如对本文有疑问, 点击进行留言回复!!
使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)
C#实现获取本地内网(局域网)和外网(公网)IP地址的方法分析
网友评论