构造函数可以是私有的吗?私有构造函数如何有用?
当前回答
是的,它可以。私有构造函数的存在是为了防止类被实例化,或者因为构造只在内部发生,例如工厂模式。更多信息请参见这里。
其他回答
是的,构造函数可以是私有的。私有构造函数阻止任何其他类实例化私有构造函数的示例
public class CustomHttpClient {
private static HttpClient customHttpClient;
/** A private Constructor prevents any other class from instantiating. */
private CustomHttpClient() {
}}
我以为有人会提到这一点(第二点),但是…私有构造函数有三种用法:
to prevent instantiation outside of the object, in the following cases: singleton factory method static-methods-only (utility) class constants-only class . to prevent sublcassing (extending). If you make only a private constructor, no class can extend your class, because it can't call the super() constructor. This is some kind of a synonym for final overloaded constructors - as a result of overloading methods and constructors, some may be private and some public. Especially in case when there is a non-public class that you use in your constructors, you may create a public constructor that creates an instance of that class and then passes it to a private constructor.
如果一个类中的所有方法都是静态的,那么私有构造函数是个好主意。
是的,构造函数可以是私有的。这个有不同的用法。其中一种用途是用于单例设计反模式,我建议您不要使用这种反模式。另一个更合理的用途是委托构造函数;你可以有一个构造函数它有很多不同的选项这是一个实现细节,所以你把它设为私有,但是你剩下的构造函数都委托给它。
作为委托构造函数的一个例子,下面的类允许您保存一个值和一个类型,但它只允许您为类型的子集这样做,因此需要将通用构造函数设置为私有,以确保只使用允许的类型。公共私有构造函数有助于代码重用。
public class MyClass {
private final String value;
private final String type;
public MyClass(int x){
this(Integer.toString(x), "int");
}
public MyClass(boolean x){
this(Boolean.toString(x), "boolean");
}
public String toString(){
return value;
}
public String getType(){
return type;
}
private MyClass(String value, String type){
this.value = value;
this.type = type;
}
}
编辑 几年后再看这个答案,我想指出,这个答案既不完整,也有点极端。单例的确是一种反模式,通常应该尽可能避免使用;然而,除了单例外,私有构造函数还有很多用途,我的回答只提到了一种。
再举几个使用私有构造函数的例子:
To create an uninstantiable class that is just a collection of related static functions (this is basically a singleton, but if it is stateless and the static functions operate strictly on the parameters rather than on class state, this is not as unreasonable an approach as my earlier self would seem to suggest, though using an interface that is dependency injected often makes it easier to maintain the API when the implementation requires larger numbers of dependencies or other forms of context). When there are multiple different ways to create the object, a private constructor may make it easier to understand the different ways of constructing it (e.g., which is more readable to you new ArrayList(5) or ArrayList.createWithCapacity(5), ArrayList.createWithContents(5), ArrayList.createWithInitialSize(5)). In other words, a private constructor allows you to provide factory function's whose names are more understandable, and then making the constructor private ensures that people use only the more self-evident names. This is also commonly used with the builder pattern. For example: MyClass myVar = MyClass .newBuilder() .setOption1(option1) .setOption2(option2) .build();
Yes.
这样您就可以控制类的实例化方式。如果将构造函数设为私有,然后创建一个返回类实例的可见构造函数方法,则可以限制创建的数量(通常是保证只有一个实例)或回收实例或其他与构造相关的任务。
执行new x()从不返回null,但是使用工厂模式,您可以返回null,甚至返回不同的子类型。
也可以将它用于没有实例成员或属性,只有静态成员或属性的类——就像在实用函数类中一样。
推荐文章
- 如何在java中格式化持续时间?(如格式H:MM:SS)
- urlencoder .encode(字符串)已弃用,我应该使用什么代替?
- javax.transaction.Transactional vs . org.springframework.transaction.annotation.Transactional
- Java 8接口方法中不允许“同步”的原因是什么?
- 如何找到Java堆大小和内存使用(Linux)?
- 使用Enum实现单例(Java)
- RabbitMQ与通道和连接之间的关系
- buildSessionFactory()配置方法在Hibernate中已弃用?
- Spring MVC -如何获得所有的请求参数在一个地图在Spring控制器?
- 如何在Java中按两个字段排序?
- 文件之间的差异。路径中的分隔符和斜杠
- 在方法参数中使用NotNull注释
- Spring MVC中处理可选参数的@RequestParam
- Tomcat:如何查找正在运行的Tomcat版本?
- “java”、“javaw”和“javaws”之间有什么区别?