4、单例模式

namespace SJ.DesignMode
{
    /// <summary>
    /// 设计模式
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {

           SingleDistance s = SingleDistance.Create();

            s.GetNum();
            SingleDistance.num++;

            SingleDistance s2 = SingleDistance.Create();
            s2.GetNum();




     }
    }

 public class SingleDistance
    {
        private static SingleDistance _singleDistance = null;

        public static int num = 0;

        private SingleDistance() { }

        public static SingleDistance Create()
        {
            if (_singleDistance == null)
            {
                _singleDistance = new SingleDistance();
            }
            return _singleDistance;
        }

        public void GetNum()
        {
            Console.WriteLine(num);
        }


    }
}
    

  

posted @ 2021-12-21 14:16  艾特-天空之海  阅读(17)  评论(0编辑  收藏  举报