单例模式中饿汉模式

来源:互联网 发布:ios手游模拟器 mac 编辑:程序博客网 时间:2024/06/11 00:27
  class Student
    {
        private static readonly Student student = new Student();//静态初始化的时候同时实例化;
        private Student() { }//将构造方法私有化,防止实例化;
        public static Student Getstudent()
        {
            return student;
        }
        public void Say()
        {
            Console.WriteLine("饿汉模式");
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            Student.Getstudent().Say();
            Console.ReadKey();
        }
    }
1 0
原创粉丝点击