码迷,mamicode.com
首页 > 其他好文 > 详细

单例模式

时间:2014-05-22 02:19:11      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:class   c   code   a   int   get   

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();
}
}
}

单例模式,布布扣,bubuko.com

单例模式

标签:class   c   code   a   int   get   

原文地址:http://www.cnblogs.com/xhyang/p/3738746.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!