构造函数可以是私有的吗?私有构造函数如何有用?
当前回答
是的。类可以有私有构造函数。即使是抽象类也可以有私有构造函数。
通过将构造函数设为私有,可以防止类被实例化以及类的子类化。
下面是私有构造函数的一些用法:
单例设计模式 限制创建实例的数量 使用静态工厂方法为对象创建提供有意义的名称 静态实用类或常量类 防止子类化 构建器设计模式,从而创建不可变类
其他回答
我以为有人会提到这一点(第二点),但是…私有构造函数有三种用法:
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.
私有构造函数用于防止实例初始化,例如您在java中使用的Math final类。单例也使用私有构造函数
是的,构造函数可以是私有的。私有构造函数阻止任何其他类实例化私有构造函数的示例
public class CustomHttpClient {
private static HttpClient customHttpClient;
/** A private Constructor prevents any other class from instantiating. */
private CustomHttpClient() {
}}
推荐文章
- 指定的子节点已经有一个父节点。你必须先在子对象的父对象上调用removeView() (Android)
- 对于一个布尔字段,它的getter/setter的命名约定是什么?
- 如何获得当前屏幕方向?
- 如何在Android中渲染PDF文件
- 如何计算一个元素在列表中出现的次数
- c++中类似于java的instanceof
- 我如何解决错误“minCompileSdk(31)指定在一个依赖的AAR元数据”在本机Java或Kotlin?
- 如何POST表单数据与Spring RestTemplate?
- Mockito中检测到未完成的存根
- 我应该如何复制字符串在Java?
- “while(true)”循环有那么糟糕吗?
- 这个方法签名中的省略号(…)是干什么用的?
- Java:如何测试调用System.exit()的方法?
- 带有返回类型的Java方法在没有返回语句的情况下编译
- Java“此语言级别不支持lambda表达式”