Skip to content

Instantly share code, notes, and snippets.

@diaolizhi
Last active October 17, 2018 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diaolizhi/a31e24a545ec7216d313e2c0f4ecac65 to your computer and use it in GitHub Desktop.
Save diaolizhi/a31e24a545ec7216d313e2c0f4ecac65 to your computer and use it in GitHub Desktop.
c# 一般情况下的单例模式
class OnlyYou
{
//构造函数
private OnlyYou()
{
}
//注意类型是 OnlyYou,并且是静态的
private static OnlyYou obj = null;
//静态代码块,名字一定要跟类名一样。无论 new 几次,只会执行一次。
static OnlyYou()
{
obj = new OnlyYou();
}
//返回唯一实例
public OnlyYou getOnlyYou()
{
return obj;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment