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

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

问题描述

构造函数可以是私有的吗?私有构造函数有什么用?

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

推荐答案

是的,构造函数可以是私有的.这有不同的用途.其中一种用途是用于 singleton 设计反模式,我建议您不要使用.另一个更合法的用途是委托构造函数;您可以拥有一个带有许多不同选项的构造函数,这实际上是一个实现细节,因此您将其设为私有,但随后您剩余的构造函数委托给它.

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

当有多种不同的方式来创建对象时,私有构造函数可能会让你更容易理解不同的构造方式(例如,这对你来说更具可读性new ArrayList(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天全站免登陆