zoukankan      html  css  js  c++  java
  • 单例模式

    namespace 单例模式
    {
    class Student
    {
    int id;
    string name;
    //1
    private Student()
    {

    }

    //2
    private static Student instance = new Student();
    //3
    public static Student Instance()
    {
    return instance;
    }


    public int Id
    {
    get { return id; }
    set { id = value; }
    }
    public string Name
    {
    get { return name; }
    set { name = value; }

    }
    public void AttendClass()
    {
    Console.WriteLine("上课");
    }
    }

    class Student2
    {
    int id;
    string name;
    //2
    private static Student2 instance ;
    //1
    private Student2()
    {

    }
    //3
    public static Student2 Instance()
    {
    if (instance == null)
    {
    instance = new Student2();
    }
    return instance;
    }


    public int Id
    {
    get { return id; }
    set { id = value; }
    }
    public string Name
    {
    get { return name; }
    set { name = value; }

    }
    public void AttendClass()
    {
    Console.WriteLine("上课");
    }
    }
    }
    namespace 单例模式
    {
    class Program
    {
    static void Main(string[] args)
    {
    //Student zs = new Student();
    //Student ls = new Student();
    //bool b=zs.GetHashCode() == ls.GetHashCode();

    Student zs = Student.Instance();
    Student ls = Student.Instance();
    bool b = zs.GetHashCode() == ls.GetHashCode();

    Student2 zs2 = Student2.Instance();
    Student2 ls2 = Student2.Instance();
    b = zs2.GetHashCode() == ls2.GetHashCode();
    }
    }
    }

  • 相关阅读:
    字符数组,字符指针,字符串常量以及其sizeof的一些总结
    Linux——makefile
    动态定义多维数组
    二叉树的前序、中序、后序遍历(非递归)
    构造函数、析构函数抛出异常的问题
    倒排索引
    宏定义
    sizeof && strlen
    搜索引擎技术原理
    最小生成树
  • 原文地址:https://www.cnblogs.com/xhyang/p/3738746.html
Copyright © 2011-2022 走看看