单例

1.确保在程序运行中一个类只有一个实例


2.实现单例的模式

构造方法设为private

內建静态实例

静态构造方法给实例做初始化


3.单例的设计模式

public class Singleton{

     private static Singleton instance;

     private Singleton(){ }

     public static Singleton Instance {

           get{

                if(instance == null) {

                     instance = new Singleton();

               }

          return instance;

         }

}

static Singleton() {

     Console.WL("        ");

}

public void SaySingleton(){

     Console.WL("         ");

}

你可能感兴趣的:(单例)