Last active
October 17, 2018 11:25
-
-
Save diaolizhi/a31e24a545ec7216d313e2c0f4ecac65 to your computer and use it in GitHub Desktop.
c# 一般情况下的单例模式
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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