使用private关键字将默认构造函数定义私有防止创建实例。
使得对象在内存中只存在一个 可参考代码。
public class SimpLeMath
{
private SimpLeMath()
{
}
private static SimpLeMath simple { get; set; }
public static SimpLeMath GetSimpLeMath(){
if(simple==null){
simple=new SimpLeMath();
return simple;
}else{
return simple;
}
}
}
按照以上代码 构造函数私有时,实例不可以创建只有调用自身的GetSimpleMath()方法。