C#私有的构造函数的作⽤
C#私有的构造函数的作⽤:当类的构造函数是私有的时候,也已防⽌C1 c1=new C1();实例化类。常见的应⽤是⼯具类和单例模式。using System;
using System.Collections.Generic;
namespace NetGraphical
{
internal class Program
{
public static void Main(string[] args)
{
Console.b1);
Console.WriteLine("--------------1");
C1 c1 = new C1();
Console.WriteLine("--------------2");
}
}
class C1
{
public static int meb1 = 10;
static C1()
{
Console.WriteLine("static constructed function:" + meb1);
meb1 = 20;
}
public C1()
{
Console.WriteLine("constructed function");
}
}
class Singleton
{
private static Singleton s = null;
private Singleton()
writeline方法的作用
{
}
public static Singleton getInstance()
{
if (s == null)
{
s = new Singleton();
}
return s;
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论