Java中的构造函数可以是私有的吗? [英] Can a constructor in Java be private?

查看:478
本文介绍了Java中的构造函数可以是私有的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

构造函数可以是私有的吗?如何使用私有构造函数?

Can a constructor be private? How is a private constructor useful?

推荐答案

是的,构造函数可以是私有的。这有不同的用途。其中一个用途是单身 设计反模式 ,我建议您反对使用。另一个更合法的用法是委托构造函数;你可以有一个构造函数,它有很多不同的选项,这是一个实现细节,所以你使它的私有,但然后你的剩余的构造函数委托给它。

Yes, a constructor can be private. There are different uses of this. One such use is for the singleton design anti-pattern, which I would advise against you using. Another, more legitimate use, is in delegating constructors; you can have one constructor that takes lots of different options that is really an implementation detail, so you make it private, but then your remaining constructors delegate to it.

代理构造函数的例子,下面的类允许你保存一个值和一个类型,但是它只允许你为一个子类型做它,所以使得通用构造函数私有是为了确保只使用允许的类型。公共私有构造函数有助于代码重用。

As an example of delegating constructors, the following class allows you to save a value and a type, but it only lets you do it for a subset of types, so making the general constructor private is needed to ensure that only the permitted types are used. The common private constructor helps code reuse.

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;
     }
}

>
从几年后的这个答案,我想指出,这个答案是不完整的,也有点极端。单身人士确实是一种反模式,一般应尽可能避免;但是,除了单例之外,还有许多私有构造函数的使用,我的回答只有一个。

Edit
Looking at this answer from several years later, I would like to note that this answer is both incomplete and also a little bit extreme. Singletons are indeed an anti-pattern and should generally be avoided where possible; however, there are many uses of private constructors besides singletons, and my answer names only one.

给一些情况使用私有构造函数:

To give a couple more cases where private constructors are used:


  1. 创建一个不可重定义的类,它只是一个相关的静态函数的集合(这基本上是一个单例,但如果它是无状态的,函数严格地依赖于参数而不是类的状态,这并不像我早期的自我似乎暗示的那样不合理,尽管使用依赖注入的接口常常使得当实现需要更大数量时更容易维护API的依赖关系或其他形式的上下文)。

  1. 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).

当有多种不同的方式来创建对象时,私有构造函数可能会更容易理解不同的方式(5)或 ArrayList.createWithCapacity(5) ArrayList.createWithContents(5) ArrayList.createWithInitialSize(5))。换句话说,私有构造函数允许你提供其名称更容易理解的工厂函数,然后使构造函数私有,确保人们只使用更不明显的名字。这也通常用于构建器模式。例如:

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();


这篇关于Java中的构造函数可以是私有的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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