简体   繁体   English

如何使用具有私有构造函数的类的实例?

[英]How to use an instance of a class that has a private constructor?

If we have class A & B, and class A's constructor is private, and we want to use an instance of A in B, how to do that ? 如果我们有类A和B,并且类A的构造函数是私有的,并且我们想在B中使用A的实例,该怎么做? I see an answer that says "provide a static method or variable that allows access to an instance created from within the class " but I didn't understand that. 我看到一个回答,说“提供一个静态方法或变量,该方法或变量允许访问从该类内部创建的实例”,但我不明白。

The code pattern you seek is called the Factory Method . 您寻找的代码模式称为Factory Method

The class provides a static method that returns an instance of its own class. 该类提供了一个静态方法,该方法返回其自己的类的实例。 Private constructors are visible to all methods (including static ones) of the class, so the static method can invoke the private constructor on the caller's behalf. 私有构造函数对于该类的所有方法(包括静态方法)都是可见的,因此静态方法可以代表调用方调用私有构造函数。

Here's an example of this pattern in action: 这是这种模式的示例:

public class A {
    private A() {
    }

    public static A create() {
        return new A();
    }
}

This is often employed in conjunction with the Singleton Pattern , which would change the above example to this: 这通常与Singleton Pattern结合使用,它将上面的示例更改为:

public class A {
    private static A INSTANCE = new A();

    private A() {
    }

    public static A getInstance() {
        return INSTANCE;
    }
}

A needs to have a public method that provides an instance of the class A, eg: A需要有一个提供类A实例的公共方法,例如:

class A {
  /*Constructors and other methods omitted*/
  public static A getInstance() {
    return new A();
  }
}

Alternatively, if B is an inner class of A (or vice-versa), then B can directly reference the constructor eg: 或者,如果B是A的内部类(反之亦然),则B可以直接引用构造函数,例如:

public class A {
    private A() {}

    public static class B {
        private A instanceOfA = new A();

        public B() {}
    }
}

A class that only has private constructors is designed so that other classes cannot instantiate it directly. 设计仅具有私有构造函数的类,以便其他类无法直接实例化它。 Presumably there is a sound reason for this. 大概有一个合理的原因。 The class may provide a factory method for instantiating the class ... or getting an existing instance of the class. 该类可以提供用于实例化该类或获取该类的现有实例的工厂方法。

If you need to change the design, the best way is to modify the class; 如果需要更改设计,最好的方法是修改类。 eg by making a constructor visible, or by adding a factory method. 例如,通过使构造函数可见,或添加工厂方法。 If you can't do that, I think it is possible to use reflection to break the visibility rules and create an instance using a private constructor. 如果您不能这样做,我认为可以使用反射来打破可见性规则并使用私有构造函数创建实例。 However, I'd only do this as a last resort ... and not before carefully analysing the consequences for the overall application. 但是,我只会在没有其他选择时才这样做,而不是在仔细分析整个应用程序的后果之前。

Private constructors are intended to make a class not to have any instance. 私有构造函数旨在使一个类不具有任何实例。 But the content can be accessed from child class using super() . 但是可以使用super()从子类访问内容。 Implementation is like this: 实现是这样的:

public class ClassA { 公共类ClassA {
private int val; 私人国际价
private ClassA(int val) 私人ClassA(int val)
{ {

  this.val = val; 

} }

public int getVal() { return val; public int getVal(){return val;
} }

} }

public class ClassB extends ClassA { 公共类ClassB扩展了ClassA {

public ClassB(int val) { super(val); 公共ClassB(int val){super(val); } } }}

... ...

ClassB b = new ClassB(4); ClassB b =新的ClassB(4);

System.out.println("value of b: " + b.getVal()); System.out.println(“ b的值:” + b.getVal());

As an example see class Calendar . 作为示例,请参见Calendar类。 To get an instance you must not call its constructor but use a static method: 要获取实例,您必须不调用其构造函数,而应使用静态方法:

Calendar rightNow = Calendar.getInstance();

source 资源

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 嘲笑具有私有构造函数的类? - mocking a class that has a private constructor? 私有类的构造函数是否必须是私有的? - Does a constructor of private class has to be private? 在java中,如何使用私有构造函数创建一个类,其超类也有一个私有构造函数? - In java, how do I make a class with a private constructor whose superclass also has a private constructor? 如何实例化具有带有参数的私有构造函数的泛型类 - How to instantiate generic class which has a private constructor with an argument 对于具有私有构造函数的类,instanceof如何返回true? - How can instanceof return true for a class that has a private constructor? 如何在具有私有构造函数的 class 中使用 @Value 或 autowire 环境? - How can I use @Value or autowire Environment in a class with private constructor? 是否可以在构造函数中使用此类实例? - Is it possible to use this class instance in the constructor? 如何在具有私有构造函数的本地内部类的外部创建实例? - How is it possible to create instance outside class of local inner class having private constructor? 如何确保不能使用反射从 class 外部创建具有私有构造函数的 class 实例? - How to make sure instance of a class with a private constructor can not be created from outside of the class using Reflection? 为什么类实例本身没有公共构造函数? - Why class instance itself has no public constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM