在类中将构造函数私有化有什么用? [英] What is the use of making constructor private in a class?

查看:104
本文介绍了在类中将构造函数私有化有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们为什么要在类中将构造函数设为私有?因为我们一直需要构造函数是公开的.

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:

  1. 只能从类本身内部的静态工厂方法中访问构造函数. Singleton也可以属于此类别.
  2. 实用程序类,仅包含静态方法.
  1. The constructor can only be accessed from static factory method inside the class itself. Singleton can also belong to this category.
  2. A utility class, that only contains static methods.

这篇关于在类中将构造函数私有化有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆