简体   繁体   English

这里的构造函数是什么?

[英]What's the constructor here?

The text of an exercise from a well known and basic Java programming textbook follows: 来自一本知名的基本Java编程教科书的练习文本如下:

'Sometimes we would like a class that has just a single unique instance. 有时候,我们希望一个类只有一个唯一的实例。 Create a class Merlin that has one attribute, theWizard, which is static and of type Merlin. 创建一个具有一个属性theWizard的类Merlin,该属性是静态的,类型为Merlin。 The class has only one constructor and two methods, as follows: * Merlin —a private constructor. 该类只有一个构造函数和两个方法,如下所示:* Merlin-私有构造函数。 Only this class can invoke this construc- tor; 只有此类可以调用此构造函数。 no other class or program can create an instance of Merlin * summon —a static method that returns theWizard if it is not null; 没有其他类或程序可以创建Merlin * summon的实例-一个静态方法,如果该方法不为null,则返回theWizard。 if theWizard is null, this method creates an instance of Merlin using the private constructor and assigns it to theWizard before returning it. 如果theWizard为null,则此方法使用私有构造函数创建Merlin的实例,并在返回之前将其分配给theWizard。 * consult—a non-static method that returns the string "Pull the sword from the stone" ' *咨询-一种非静态方法,返回字符串“从石头上拔剑”'

Were you able to understand what the author is asking in the point 'Merlin -a private constructor' ? 您是否能够理解作者在“ Merlin-一个私有构造函数”方面的要求? I know what a constructor is as well as what a private method is. 我知道什么是构造函数以及什么是私有方法。 But here what's the answer ? 但是,答案是什么? I thought of something like 我想到了类似的东西

public class Merlin {
    private static Merlin theWizard;

    /*public Merlin()
    {
        ???
    }*/

    private Merlin()
    {
         this();
    }

    public static Merlin summon(Merlin theWizard) { 
        if (theWizard == null) {
            theWizard = new Merlin(); 
        return theWizard; 
    }

    public String consult() {
        return "Pull the sword from the stone"; 
    }
}

and of course JavaBeans says 'private Merlin()' is recursive. 而且JavaBeans当然说“私有Merlin()”是递归的。 So what do I do ? 那我该怎么办? The next exercise is related to the above one and could help you to understand what the answer is; 下一个练习与上面的练习有关,可以帮助您理解答案是什么。 here it is: 这里是:

  1. Create a program that tests the class Merlin described in the previous exercise. 创建一个程序来测试上一练习中描述的Merlin类。
    • Use the toString method to verify that a unique instance has been created. 使用toString方法来验证是否已创建唯一实例。

Thank you very much for all that you will be able to tell me. 非常感谢您将能够告诉我的一切。

This is an instance of the Singleton Pattern; 这是单例模式的一个实例; in your case, just replace this() with super() to use the superclass's constructor (technically, if you don't place a super constructor call, the compiler will insert one for you; so you could just remove it). 在您的情况下,只需用super()替换this()即可使用超类的构造函数(从技术上讲,如果您不进行超级构造函数调用,编译器会为您插入一个;因此您可以删除它)。

Food for thought: 值得深思的:

  • why is the Merlin class private ? 为什么Merlin类是private
  • why is the Merlin class static ? 为什么Merlin类是static
  • where's the rest of your code? 您其余的代码在哪里?

Get rid of this() : 摆脱this()

public class Merlin {

    private static Merlin theWizard;

    private Merlin() {}
    ...
}

this followed by parens is the syntax used to call a constructor of the same class, since you are not specifying any arguments inside the parens then you're referring to the no-argument instructor, which is the constructor this is being called from, so the constructor would call itself recursively until it runs out of stack space. this之后是parens,是用于调用同一类的构造函数的语法,因为您没有在parens中指定任何参数,所以您引用的是无参数讲师,这是从中调用的构造函数,因此构造函数将递归调用自身,直到耗尽堆栈空间。 The IDE is checking for this problem and trying to warn you about it. IDE正在检查此问题,并试图警告您。

Calling super() in the constructor is allowed (it calls the no-argument constructor in the superclass, which here is java.lang.Object) but not required, the compiler will insert a call to super if you don't. 允许在构造函数中调用super() (它在超类(这里是java.lang.Object)中调用无参数构造函数),但不是必需的,否则编译器将插入对super的调用。

By the way you can't have multiple overloads of a constructor or method with the same argument list. 顺便说一句,对于具有相同参数列表的构造函数或方法,不能有多个重载。 You can't have a public no-arg constructor and a private no-arg constructor. 您不能有一个公共的无参数构造函数和一个私有的无参数构造函数。

The author is trying to describe a class that has control over how its instances are created, it makes its constructor private so that only the class itself is allowed to instantiate an object of that class. 作者试图描述一个可以控制实例创建方式的类,将其构造函数设为私有,以便仅允许类本身实例化该类的对象。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM