在java中从另一个调用一个构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2373988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:40:49  来源:igfitidea点击:

call one constructor from another in java

javaconstructor

提问by giri

This was the question asked in interview. Can we call one constructor from another if a class has multiple constructors in java and when?How can I call I mean syntax?

这是面试时问的问题。如果一个类在java中有多个构造函数,我们可以从另一个构造函数调用一个构造函数吗?我怎么称呼我的意思是语法?

采纳答案by Sean

You can, and the syntax I know is

你可以,我知道的语法是

this(< argument list >);

You can also call a super class' constructor through

您还可以通过调用超类的构造函数

super(< argument list >);

Both such calls can only be done as the first statement in the constructor (so you can only call one other constructor, and before anything else is done).

这两个调用都只能作为构造函数中的第一条语句完成(因此您只能调用另一个构造函数,并且在其他任何事情完成之前)。

回答by Oli

this(other, args);

回答by Synox

example:

例子:

public class FileDb {

  /**
   * 
   */
  public FileDb() {
    this(null);
  }

  public FileDb(String filename) {
    // ...
  }

}

回答by Peter Lang

Yes, you can do that.

是的,你可以这样做。

Have a look at the ArrayListimplementation for example:

看一下ArrayList实现,例如:

public ArrayList(int initialCapacity) {
    super();
    if (initialCapacity < 0)
        throw new IllegalArgumentException("Illegal Capacity: "+
                                           initialCapacity);
    this.elementData = new Object[initialCapacity];
}

/**
 * Constructs an empty list with an initial capacity of ten.
 */
public ArrayList() {
    this(10);
}

The second constructor calls the first one with a default capacityof ten.

第二个构造函数default capacity以 10 的a 调用第一个构造函数。

回答by polygenelubricants

FYI, this is called the telescoping/telescopic constructor pattern.

仅供参考,这称为伸缩/伸缩构造函数模式

It's discussed in JLS 8.8.7.1 Explicit Constructor Invokations

JLS 8.8.7.1 显式构造函数调用中对此进行了讨论

  • Alternate constructor invocationsbegin with the keyword this(possibly prefaced with explicit type arguments). They are used to invoke an alternate constructor of the same class.
  • Superclass constructor invocationsbegin with either the keyword super(possibly prefaced with explicit type arguments) or a Primary expression. They are used to invoke a constructor of the direct superclass.
  • 替代构造函数调用以关键字this开头(可能以显式类型参数开头)。它们用于调用同一类的备用构造函数。
  • 超类构造函数调用以关键字super(可能以显式类型参数开头)或主表达式开头。它们用于调用直接超类的构造函数。

回答by Yishai

None of the answers are complete, so I'm adding this one to fill in the blanks.

没有一个答案是完整的,所以我添加了这个来填补空白。

You can call one constructor from another in the same class, or call the super class, with the following restrictions:

您可以从同一类中的另一个构造函数调用一个构造函数,或调用超类,但有以下限制:

  1. It has to be the first line of code in the calling constructor.
  2. It cannot have any explicit or implicit reference to this. So you cannot pass an inner class (even an anonymous one if it references any instance methods), or the result of a non-static method call, as a parameter.
  1. 它必须是调用构造函数中的第一行代码。
  2. 它不能对 有任何显式或隐式引用this。因此,您不能将内部类(即使是匿名类,如果它引用任何实例方法)或非静态方法调用的结果作为参数传递。

The syntax (as mentioned by others) is:

语法(如其他人所述)是:

MyClass() {
   someInitialization();
}

MyClass(String s) { 
     this();
     doSomethingWithS(s);
}