且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

单例类与静态方法

更新时间:2023-11-13 14:02:58

如果getInstance()不是静态的,你只能为该类的对象调用它。但是当你称它时,你通常没有那个阶级的对象;相反,你想要检索它或创建唯一的对象。因此该方法必须是静态的。



顺便说一下, instanceFlag 是多余的。你可以同样测试单个指针不等于NULL。
If the getInstance() were not static you could only call it for an object of the class. But at the time you call it you typically have no object of that class; instead you want to either retrieve it or create the one and only object. And hence the method must be static.

And, by the way, the instanceFlag is superfluous. You could equally well test the single pointer on being not equal to NULL.