溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Singleton設計模式(單實例)

發(fā)布時間:2020-05-25 13:38:56 來源:網絡 閱讀:330 作者:1473348968 欄目:編程語言
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//Singleton設計模式(單實例)
namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            //案例:
            Singleton s = Singleton.getSingleton();
            s.name = "asd";
            Singleton s1 = Singleton.getSingleton();
            s1.ShowMsg();
            Console.ReadKey();
        }
    }
    class Singleton
    {
        //靜態(tài)類(自己)
        private static Singleton singleton = null;
        //私有構造函數(shù)
        private Singleton() { }
        public static Singleton getSingleton()
        {
            if (singleton == null)
                singleton = new Singleton();//實例化
            return singleton;
        }

        public string name { get; set; }
        public void ShowMsg()
        {
            Console.WriteLine(name);
        }
    }
}

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI