Why do we need a private constructor?

后端 未结 10 565
一整个雨季
一整个雨季 2020-11-30 18:20

If a class has a private constructor then it can\'t be instantiated. So, if I don\'t want my class to be instantiated and still use it, then I can make it static.

Wh

10条回答
  •  臣服心动
    2020-11-30 18:46

    Regarding singletons - singleton is a design pattern used when the environment and requirements satisfy similar motivations for the pattern's use; static classes are a language feature.

    As LBushkin's answer discusses, while some of the goals of using singleton can be met using static classes, a particular implementation of singleton may exceed the feature set of static classes alone.

提交回复
热议问题