在类中将构造函数设为私有有什么用? [英] What is the use of making constructor private in a class?
本文介绍了在类中将构造函数设为私有有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么要在类中将构造函数设为私有?因为我们总是需要构造函数是公开的.
Why should we make the constructor private in class? As we always need the constructor to be public.
推荐答案
您可能需要私有构造函数的一些原因:
Some reasons where you may need private constructor:
- 只能从类本身内部的静态工厂方法访问构造函数.单身人士也可以属于这一类.
- 实用程序类,仅包含静态方法.
- The constructor can only be accessed from static factory method inside the class itself. Singleton can also belong to this category.
- A utility class, that only contains static methods.
这篇关于在类中将构造函数设为私有有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文